以关键字序列(256,301,751,129,937,863,742,694,076,438)为例,实现直接插入排序,折半插入排序 和希尔排序。
1 #include "stdio.h" 2 #define MAX 100 3 4 typedef int KeyType; 5 typedef char InfoType[10]; 6 typedef struct 7 { 8 KeyType key; 9 InfoType data; 10 }RecType; 11 12 //直接按照递增有序排列 13 void Straight_Insert_sort(RecType R[],int n) 14 { 15 int i,j,k; 16 RecType temp; 17 for(i=1;i<n;i++) 18 { 19 temp=R[i];//设立哨兵 20 j=i-1; 21 while(j>=0&&temp.key <R[j].key ) 22 { 23 R[j+1]=R[j]; 24 j--; 25 } 26 R[j+1]=temp; 27 printf("i=%d,",i); 28 printf("插入%3d的结果为:",temp); 29 for(k=0;k<n;k++) 30 printf(" %3d ",R[k].key ); 31 printf("\n"); 32 } 33 } 34 35 //折半插入排序 36 void Binary_Insert_sort(RecType R[],int n) 37 { 38 int i,j,k,low,mid,high; 39 RecType temp; 40 for(i=1;i<n;i++) 41 { 42 43 temp=R[i]; 44 low=0;high=i-1; 45 printf("\n第%d次要插入的关键字为:%d",i,temp); 46 while(low<=high) 47 { 48 mid=(low+high)/2; 49 printf("\nlow=%2d ,high=%2d ,mid=%2d R[%d]=%d ",low,high,mid,mid,R[mid].key ); 50 if(temp.key <R[mid].key ) 51 high=mid-1; 52 else low=mid+1; 53 54 } 55 56 for(j=i-1;j>high;j--) 57 R[j+1]=R[j]; 58 R[high+1]=temp; 59 printf("\n"); 60 61 printf("第%d次插入后的结果为:",i); 62 for(k=0;k<n;k++) 63 { 64 65 printf("%3d ",R[k].key ); 66 } 67 printf("\n"); 68 69 } 70 } 71 72 //希尔排序 73 void ShellInsert(RecType R[],int n,int dk) 74 { 75 RecType temp; 76 for(int i=dk;i<n;i++) 77 78 { 79 temp =R[i]; 80 81 for(int j=i-dk;j>=0&&temp.key <R[j].key ;j=j-dk) 82 R[j+dk]=R[j]; 83 R[j+dk]=temp; 84 } 85 } 86 87 void main() 88 { 89 int i,k,n=10; 90 int a[]={256,301,751,129,937,863,742,694,76,436}; 91 RecType R[MAX]; 92 for(i=0;i<n;i++) 93 R[i].key =a[i]; 94 printf("\n\n 初始关键字: "); 95 for(k=0;k<n;k++) 96 printf(" %3d ",R[k].key ); 97 printf("\n\n"); 98 Straight_Insert_sort(R,n); 99 printf("\n 最后结果为: "); 100 for(k=0;k<n;k++) 101 printf(" %3d ",R[k].key ); 102 printf("\n"); 103 104 105 for(i=0;i<n;i++) 106 R[i].key =a[i]; 107 printf("\n 初始关键字: "); 108 for(i=0;i<n;i++) 109 printf(" %3d",R[i].key ); 110 111 printf("\n"); 112 Binary_Insert_sort(R,n); 113 printf("\n"); 114 printf("最后结果:\n "); 115 for(i=0;i<n;i++) 116 printf("R[%d] ",i); 117 printf("\n "); 118 for(i=0;i<n;i++) 119 printf(" %d ",R[i]); 120 printf("\n"); 121 122 for(i=0;i<n;i++) 123 R[i].key =a[i]; 124 125 126 printf("\n 初始关键字: "); 127 for(i=0;i<n;i++) 128 printf(" %3d",R[i].key ); 129 130 printf("\n"); 131 132 ShellInsert(R,n,5); 133 printf("\n第一趟希尔排序的结果:\n "); 134 135 for(i=0;i<n;i++) 136 printf("R[%d] ",i); 137 printf("\n "); 138 for(i=0;i<n;i++) 139 printf(" %d ",R[i]); 140 printf("\n\n"); 141 142 printf("第二趟希尔排序的结果:\n "); 143 ShellInsert(R,n,3); 144 for(i=0;i<n;i++) 145 printf("R[%d] ",i); 146 printf("\n "); 147 for(i=0;i<n;i++) 148 printf(" %d ",R[i]); 149 printf("\n\n"); 150 printf("第三趟希尔排序的结果:\n "); 151 for(i=0;i<n;i++) 152 printf("R[%d] ",i); 153 printf("\n "); 154 ShellInsert(R,n,1); 155 for(i=0;i<n;i++) 156 printf(" %d ",R[i]); 157 printf("\n\n"); 158 printf("最后结果为:\n "); 159 for(i=0;i<n;i++) 160 printf("R[%d] ",i); 161 printf("\n "); 162 for(i=0;i<n;i++) 163 printf(" %d ",R[i]); 164 printf("\n\n"); 165 166 }