直接插入排序
![在这里插入图片描述](https://img-blog.csdnimg.cn/2018120410523877.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1daTDk5NQ==,size_16,color_FFFFFF,t_70)
public class Sort {
public static void InsertSort(int arr[]){
int i,j,key;
for(i = 1;i < arr.length;i++){
key = arr[i]; //把要排的元素标记出来
for(j = i-1;j >= 0;j--){ //从后往前找合适自己的位置
if(key > arr[j]){
break;
}else{
arr[j+1] = arr[j];
}
}
arr[j+1] = key;
}
}
public static void print(int arr[]){
int i = 0;
for(i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
int []arr = new int[]{3,2,5,7,6,1};
InsertSort(arr);
print(arr);
}
}
希尔排序
public static void _InsertSort(int arr[],int gap){
int key;
int i, j;
for (i = gap; i < arr.length; i++){
key = arr[i];
for (j = i - gap; j >= 0; j-=gap){
if (key >= arr[j]){
break;
}
else{
arr[j + gap] = arr[j];
}
}
arr[j + gap] = key;
}
}
public static void ShellSort(int arr[]){
int gap = arr.length;
while(true){
gap = gap/3+1;
_InsertSort(arr,gap);
if(gap==1){
break;
}
}
}