/**
* 冒泡
*
* @param a
* @param length
*/
public void bubbleSort(int a[], int length) {
System.out.println("---------冒泡排序---------");
int x;
// i从0开始,所以i+j和i+j+1比较,j小于length - 1 - i
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - 1 - i; j++) {
if (a[i + j] > a[i + j + 1]) {
x = a[i + j];
a[i + j] = a[i + j + 1];
a[i + j + 1] = x;
}
}
System.out.println(i + " " + Arrays.toString(a));
}
// i从1开始,所以i+j和i+j-1比较,j小于length - i
for (int i = 1; i < length; i++) {
for (int j = 1; j < length - i; j++) {
if (a[i + j] < a[i + j - 1]) {
x = a[i + j];
a[i + j] = a[i + j - 1];
a[i + j - 1] = x;
}
}
System.out.println(i + " " + Arrays.toString(a));
}
}
/**
* 直接插入算法
*
* @param a
*/
public void insertSort(int a[]) {
System.out.println("---------直接插入---------");
int temp;// 记录当前许插入的值
for (int i = 1; i < a.length; i++) {
temp = a[i];
for (int j = i - 1; j >= 0; j--) {
if (a[j] > temp) {
a[j + 1] = a[j];
} else {
break;
}
a[j] = temp;
}
System.out.println(i + " " + Arrays.toString(a));
}
}
/**
* 选择插入
*
* @param a
*/
public void selectSort(int a[]) {
System.out.println("---------选择插入---------");
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
temp = a[i];
if (a[j] < a[i]) {
a[i] = a[j];
a[j] = temp;
}
}
System.out.println(i + " " + Arrays.toString(a));
}
}
/**
* 选择插入
*
* @param a
*/
public void shellSort(int a[]) {
System.out.println("---------希尔插入---------");
int temp;
int addNum = a.length / 2;
while (addNum > 0) {
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length - addNum; j = j + addNum) {
temp = a[j];
if (a[j] > a[j + addNum]) {
a[j] = a[j + addNum];
a[j + addNum] = temp;
}
}
System.out.println(addNum + " " + i + " " + Arrays.toString(a));
}
addNum = addNum / 2;
}
}