Selection sort
Java code for the Selection sort algorithm:
// The main method for selection sort
private static void selectionSort(int[] array){
for(int i = 0; i < array.length; i++){
int min = indexOfSmallest(array, i);
swap(array, i, min); // swaps two elements in array
}
}
// Returns the index of the smallest number
private static int indexOfSmallest(int[] array, int startIndex){
int smallest=startIndex;
for(int j = startIndex+1; j < array.length; j++){
if(array[j] < array[smallest]){
smallest = j;
}
}
return smallest;
}