1、 直接插入排序
(1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。如此反复循环,直到全部排好顺序。
(2)实例
(3)用java实现
packagecom.njue;
publicclassinsertSort {
publicinsertSort(){
inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
inttemp=0;
for(inti=1;i
intj=i-1;
temp=a[i];
for(;j>=0&&temp
a[j+1]=a[j];//将大于temp的值整体后移一个单位
}
a[j+1]=temp;
}
for(inti=0;i
System.out.println(a[i]);
}
}
2、希尔排序(最小增量排序)
(1)基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。
(2)实例:
(3)用java实现
publicclassshellSort {
publicshellSort(){
inta[]={1,54,6,3,78,34,12,45,56,100};
doubled1=a.length;
inttemp=0;
while(true){
d1= Math.ceil(d1/2);
intd=(int) d1;
for(intx=0;x
for(inti=x+d;i
intj=i-d;
temp=a[i];
for(;j>=0&&temp