选择排序
/**
* 选择排序
* <p>1、左边先有序</p>
* @param array
*/
public void selectSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int tmp = i;
for (int j = i + 1; j < array.length; j++) {
if (array[tmp] > array[j]) {
tmp = j;
}
}
if (tmp != i) {
int tmpV = array[tmp];
array[tmp] = array[i];
array[i] = tmpV;
}
System.out.println("I: " + i + ";J: " + tmp + " arr:" + Arrays.toString(array));
}
}
冒泡排序
/**
* 冒泡排序
* <p>1、右边先有序</p>
* @param a 参与排序的数组
*/
public void sortB(int[] array) {
for (int i = array.length - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
System.out.println("I:" + i + "J: " + j + " arr:" + Arrays.toString(array));
}
}
}
插入排序
/**
* 插入排序
* @param array
*/
public void insertSort(int[] array) {
int in = 0, out = 0;
for (out =1; out < array.length; out++) {
int tmp = array[out];
in = out;
while (in > 0 && array[in - 1] >= tmp) {
array[in] = array[in - 1];
--in;
}
array[in] = tmp;
System.out.println("in: " + in + " out: " + out + " array: " + Arrays.toString(array));
}
System.out.println("in: " + in + " out: " + out + " array: " + Arrays.toString(array));
}