####一、排序
七大排序
- 插入排序—直接插入排序(Straight Insertion Sort),原理都是从第二位开始比较前面一位的大小,小了就跟前面的位置替换了一下位置,直到结束。
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int temp = array[i];
int j = i - 1;
for (; j >= 0 && array[j] > temp; j--) {
//将大于temp的值整体后移一个单位
array[j + 1] = array[j];
}
array[j + 1] = temp;
}
System.out.println(Arrays.toString(array) + " insertSort");
}
复制代码
- 希尔排序
public static void shellSort(int[] array) {
int i;
int j;
int temp;
int gap = 1;
int len = array.length;
while (gap < len / 3) { gap = gap * 3 + 1; }
for (; gap > 0; gap /= 3) {
for (i = gap; i < len; i++) {
temp = array[i];
for (j = i - gap; j >= 0 && array[j] > temp; j -= gap) {
array[j + gap] = array[j];
}
array[j + gap] = temp;
}
}
System.out.println(Arrays.toString(array) + " shellSort");
}
复制代码
相关资源网站:http://blog.csdn.net/qy1387/article/details/7752973