Data Structures & Algorithms Lecture Notes

30 October 2008 • Sorting Basics


template 
void
selection_sort(T a[], int n) {

  // Re-arrange the array elements a[0..n-1] 
  // so they are in ascending order.

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


This page last modified on 24 January 2006.