Bubble sort
core idea: a stability sorting algorithm .find a max number and put it in last position by compare.
Introduce:
Begin A:
starting from the first index , compare the first and the second.
if the first greater than the second , they are swaped.
then compare the second and the third ......
finally ,the max number will put in the last position(n)and n=n -1Repeat A until n quals 0. so The array is sorted
Code
private static void bubbleSort(int[] a) {
boolean isSort = true;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
System.out.println(j + 1);
if (a[j] > a[j + 1]) {
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
isSort = false;
}
}
if (isSort) {
return;
}
isSort = true;
}
}