1 插入排序
1.1、直接插入排序
package org.com.sort;
public class InsertSort {
public static void insert_sort(int a[], int n) {
for(int i = 1; i < n; i++) {
if(a[i] < a[i-1]) {
int j = i - 1;
int temp = a[i];
while(j >= 0 && a[j] > temp) {
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
}
}
public static void main(String[] args) {
int a[] = {4,3,2,1};
insert_sort(a, a.length);
for(int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println();
}
}
1.2 希尔排序
标准希尔排序:
package org.com.sort;
public class ShellInsertSort {
public static void shell_insert_sort1(int a[], int n) {
for(int dk = n/2; dk > 0; dk /= 2) {
for(int i