Loading…
Loading…
Finds the minimum of the unsorted region on each pass and places it at the front.
Start selection sort.
1void selectionSort(int[] arr) {2 int n = arr.length;3 for (int i = 0; i < n - 1; i++) { // fill position i4 int minIdx = i; // assume i is smallest5 for (int j = i + 1; j < n; j++)6 if (arr[j] < arr[minIdx]) minIdx = j; // found a smaller one7 int temp = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = temp; // swap min into place8 // position i now sorted9 }10}