/**
* In each iteration the minimum element is bubbled out.
* After Step1 the minimum element in array [0..n-1] occupies first place
* After Step2 the minimum element in rest of the array [1..n-1] occupies second place.
* After Step3 the minimum element in rest of the array [2..n-1] occupies third place.
* So on..
* @param list
*/ public static void bubbleSort(int[] list) {
for (int i = 0 ; i < list.length - 1 ; i ++) { for (int j = i + 1 ; j < list.length ; j ++) { if (list[i] > list[j]) {
list[i] = list[i] + list[j] - (list[j] = list[i]);
}
}
/**
* Sorts and returns the time taken in milliseconds.
*
* @param list
* @return
*/ public static double bubbleSortTimeTaken(int[] list) { long startTime = System.nanoTime();
bubbleSort(list); long timeTaken = System.nanoTime() - startTime; return timeTaken * Math.pow(10, -6);
}
}