12.returnbinarySearch(a,low,mid-1,key);
13.else
14.returnbinarySearch(a,mid+1,high,key);
15.}
16.
17.//折半插入排序
18.voidbinaryInsertSort(int*a,intn)
19.{
20.inti,j,site,temp;
21.for(i=2;i<=n;i++){
22.//1.折半查找要插入的位置
23.site=binarySearch(a,1,i,a[i]);
24.temp=a[i];
25.//2.移位
26.for(j=i;j>site;j--)
27.a[j]=a[j-1];
28.//3.插入a[i]
29.a[site]=temp;
30.}
31.}
32.
33.voidmain()
34.{
35.inti;
36.inta[7]={0,3,5,8,9,1,2};//不考虑a[0]
37.binaryInsertSort(a,6);
38.for(i=1;i<=6;i++)
39.printf("%-4d",a[i]);
40.printf("\n");
41.}
viewplaincopytoclipboardprint?
1.#include
2.#defineMAX10000
3.
4.//静态链表
5.typedefstruct
6.{
7.intkey;//关键字
8.intnext;//指向下一个关键字的下标
9.}Node,*PNode;
10.
11.//表插入排序
12.voidtableInsertSort(PNodelist,intn)
13.{
14.intp,head;
15.inti;
16.//初始化
17.list[0].next=1;
18.list[1].next=0;
19.
20.//逐个插入
21.for(i=2;i<=n;i++){
22.head=0;
23.p=list[0].next;
24.//遍历链表,寻找插入位置
25.while(p!
=0&&list[p].key<=list[i].key){
26.head=p;
27.p=list[p].next;
28.}
29.if(p==0){//插入的值是最大值
30.list[i].next=p;
31.list[head].next=i;
32.}else{
33.list[i].next=p;
34.list[head].next=i;
35.}
36.
37.}
38.}
39.
40.voidmain()
41.{
42.intp;
43.Nodelist[7]={MAX,0,3,0,5,0,8,0,9,0,1,0,2,0};
44.tableInsertSort(list,6);
45.p=list[0].next;
46.while(p!
=0){
47.printf("%-4d",list[p].key);
48.p=list[p].next;
49.}
50.printf("\n");
51.}
viewplaincopytoclipboardprint?
1.#include
2.
3.//一趟增量为dk的希尔插入排序
4.voidshellInsert(int*a,intn,intdk)
5.{
6.inti,j,temp;
7.for(i=dk+1;i<=n;i+=dk){
8.temp=a[i];
9.for(j=i-dk;j>=0;j-=dk)
10.if(a[j]>temp)
11.a[j+dk]=a[j];
12.else
13.break;
14.a[j+dk]=temp;
15.}
16.}
17.
18.//希尔排序
19.voidshellSort(int*a,intn)
20.{
21.inti;
22.intdk[]={5,4,3,2,1};
23.for(i=0;i<5;i++)
24.shellInsert(a,6,dk[i]);
25.}
26.
27.voidmain()
28.{
29.inti;
30.inta[7]={0,3,5,8,9,1,2};//不考虑a[0]
31.shellSort(a,6);
32.for(i=1;i<=6;i++)
33.printf("%-4d",a[i]);
34.printf("\n");
35.}