Selection sort int minSubscript; for (int i = 0; i < n-1; i++) {. minSubscript = i; for (int
j = i+1; j < n; j++) if (list[j] < list[minSubscript]). minSubscript = j; swap(list[i] ...
// Bubble sort for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (list[j] > list[j+1]) swap(list[j], list[j+1]);
1
// Selection sort int minSubscript; for (int i = 0; i < n-1; i++) { minSubscript = i; for (int j = i+1; j < n; j++) if (list[j] < list[minSubscript]) minSubscript = j; swap(list[i], list[minSubscript]); }
2
// Insertion sort for (int i = 1; i < n; i++) { int current = list[i]; int j = i; while (j > 0 && list[j-1] > current) { list[j] = list[j-1]; j--; } list[j] = current; }
3
// Shellsort Initialize h; // The increments for (int k = 0; k < INCREMENT_COUNT; k++) { increment = h[k]; for (int i = increment; i < n; i++) { int current = list[i]; int j = i; while (j >= increment && list[j-increment] > current) { temp[j] = temp[j-increment]; j -= increment; } list[j] = current; } }
4
// A pivot finding scheme for quicksort // From Aho, Hopcroft and Ullman’s "Data Structures and Algorithms" int findPivot(int[] list, int min, int max) { int firstKey = list[min]; for (int i = min+1; i firstKey) return i; else if (list[i] < firstKey) return min; return ALL_KEYS_EQUAL; }
5
// Partitioning for quicksort // From Aho, Hopcroft and Ullman’s "Data Structures and Algorithms" int partition(int[] list, int min, int max, int pivot) { int l = min; int r = max; do { swap(list[l], list[r]); // Pointless the first time through while (list[l] < pivot) l++; while (list[r] >= pivot) r--; } while (l < r); return(l); // l refers to first element >= pivot }
6
// Quicksort // From Aho, Hopcroft and Ullman’s "Data Structures and Algorithms" // Initially call with min = 0, max = n-1 void quickSort(int[] list, int min, int max) { int pivotIndex = findPivot(min, max); if (pivotIndex != ALL_KEYS_EQUAL) { int pivot = list[pivotIndex]; int split = partition(list, min, max, pivot); quickSort(list, min, split-1); quickSort(list, split, max); } }
7