Data Structures & Algorithms Lecture Notes

30 October 2008 • Sorting Basics


template <typename T>
void
bubble_sort(T a[], int n) {

  // Re-arrange the array elements a[0..n-1] 
  // to be in ascending order.

  for (int i = 0; i < n - 1; i++)
    for (int min = n - 1; min > i; min--)
      if (a[min - 1] > a[min])
	std::swap(a[min - 1], a[min]);
  }


This page last modified on 24 January 2006.