/**
* In each iteration find the minimum element and swap with
* first element in the list. Repeat the same process with
* next element until there are no more elements.
*
* @param list
*/ public static void selectionSort(int[] list) {
int min = 0; for (int i = 0 ; i < list.length - 1 ; i ++) {
min = i; for (int j = i + 1 ; j < list.length ; j ++) { if (list[j] < list[min]) {
min = j;
}
}
list[i] = list[i] + list[min] - (list[min] = list[i]);
System.out.println("Step " + (i+1) + " : " + Arrays.toString(list));
}
}
/**
* Sorts and returns the time taken in milliseconds.
*
* @param list
* @return
*/ public static double selectionSortTimeTaken(int[] list) { long startTime = System.nanoTime();
selectionSort(list); long timeTaken = System.nanoTime() - startTime; return timeTaken * Math.pow(10, -6);
}
}