1 #include<stdio.h>
2 #include<math.h>
3 #include<iostream.h>
4 #include<stdlib.h>
5 #define OK 1
6 #define FALSE 0
7 #define MAX_NUM 100
8 typedef int Status;
9 typedef int ElemType;
10 typedef struct SqList
11 {
12 ElemType r[MAX_NUM];
13 int length;
14 }SqList;
15 typedef SqList HeapType;
16 Status Exchange(ElemType &a,ElemType &b)
17 {
18 ElemType t;
19 t=a;a=b;b=t;
20 return OK;
21 }
22 //直接插入排序
23 Status InsertSort(SqList &L)
24 {
25 int i,j;
26 for(i=2;i<=L.length;i++)
27 if(L.r[i]<L.r[i-1])
28 {
29 L.r[0]=L.r[i];
30 L.r[i]=L.r[i-1];
31 for(j=i-1;L.r[0]<L.r[j];j--)
32 L.r[j+1]=L.r[j];
33 L.r[j+1]=L.r[0];
34 }
35 return OK;
36 }
37 //希尔排序
38 void ShellInsert(SqList &L,int dk)
39 {
40 int i,j;
41 for(i=dk+1;i<=L.length;i++)
42 if(L.r[i]<L.r[i-dk])
43 {
44 L.r[0]=L.r[i];
45 for(j=i-dk;j>0&&L.r[0]<L.r[j];j-=dk)
46 L.r[j+dk]=L.r[j];
47 L.r[j+dk]=L.r[0];
48 }
49 }
50 Status ShellSort(SqList &L)
51 {
52 int i,j,t,n,dlta[10];
53 n=log(L.length)/log(2);
54 printf("请输入你要排序的趟数(系统将根据相关规则定义递增序列,但不能大于%d):\n",n);
55 cin>>t;
56 for(j=1,i=t-1;i>=0;i--,j<<=1)
57 dlta[i]=j+1;
58 dlta[t-1]--;
59 for(i=0;i<t;i++)
60 ShellInsert(L,dlta[i]);
61 return OK;
62 }
63 //冒泡排序
64 Status MPSort(SqList &L)
65 {
66 int i,j;
67 for(i=1;i<L.length;i++)
68 for(j=1;j<L.length;j++)
69 if(L.r[j]>L.r[j+1])
70 Exchange(L.r[j],L.r[j+1]);
71 return OK;
72 }
73 //快速排序
74 int Partition(SqList &L,int low,int high)
75 {
76 int pivotkey=L.r[low];
77 L.r[0]=L.r[low];
78 while(low<high)
79 {
80 while(low<high&&L.r[high]>=pivotkey) high--;
81 L.r[low]=L.r[high];
82 while(low<high&&L.r[low]<=pivotkey) low++;
83 L.r[high]=L.r[low];
84 }
85 L.r[low]=L.r[0];
86 return low;
87 }
88 void Qsort(SqList &L,int low,int high)
89 {
90 if(low<high)
91 {
92 int pivotloc=Partition(L,low,high);
93 Qsort(L,low,pivotloc-1);
94 Qsort(L,pivotloc+1,high);
95 }
96 }
97 Status QuickSort(SqList &L)
98 {
99 Qsort(L,1,L.length);
100 return OK;
101 }
102 //简单选择排序
103 int SelectMinKey(SqList L,int i)
104 {
105 int k;
106 for(k=i;i<L.length;)
107 if(L.r[k]>L.r[++i])
108 k=i;
109 return k;
110 }
111 Status SelectSort(SqList &L)
112 {
113 int i,j;
114 for(i=1;i<L.length;i++)
115 {
116 j=SelectMinKey(L,i);
117 if(i!=j) Exchange(L.r[i],L.r[j]);
118 }
119 return OK;
120 }
121 //堆排序
122 typedef SqList HeapType;
123 void HeapAdjust(HeapType &H,int s,int m)
124 {
125 int j;
126 ElemType rc=H.r[s];
127 for(j=2*s;j<=m;j*=2)
128 {
129 if(j<m&&H.r[j]<H.r[j+1]) j++;
130 if(rc>=H.r[j]) break;
131 H.r[s]=H.r[j];
132 s=j;
133 }
134 H.r[s]=rc;
135 }
136 Status HeapSort(HeapType &H)
137 {
138 int i;
139 for(i=H.length/2;i>0;i--)
140 HeapAdjust(H,i,H.length);
141 for(i=H.length;i>1;i--)
142 {
143 Exchange(H.r[1],H.r[i]);
144 HeapAdjust(H,1,i-1);
145 }
146 return OK;
147 }
148 //归并排序
149 void Merge(ElemType SR[],ElemType TR[],int i,int m,int n)
150 {
151 int j,k;
152 for(j=m+1,k=i;i<=m&&j<=n;k++)
153 {
154 if(SR[i]<SR[j]) TR[k]=SR[i++];
155 else TR[k]=SR[j++];
156 }
157 while(i<=m) TR[k++]=SR[i++];
158 while(j<=n) TR[k++]=SR[j++];
159 }
160 void MSort(ElemType SR[],ElemType TR1[],int s,int t)
161 {
162 int m;
163 ElemType TR2[MAX_NUM];
164 if(s==t) TR1[s]=SR[s];
165 else
166 {
167 m=(s+t)/2;
168 MSort(SR,TR2,s,m);
169 MSort(SR,TR2,m+1,t);
170 Merge(TR2,TR1,s,m,t);
171 }
172 }
173 Status MergeSort(SqList &L)
174 {
175 MSort(L.r,L.r,1,L.length);
176 return OK;
177 }
178 //主函数
179 Status main()
180 {
181 SqList L;
182 int i,t,d;
183 loop:puts("请输入待排序数组的元素个数:");
184 cin>>L.length;
185 puts("请输入你要排序的数组:");
186 for(i=1;i<=L.length;i++)
187 cin>>L.r[i];
188 puts("请选择你想要使用的排序方式:1.直接插入排序,2.希尔排序,3.冒泡排序,\n4.快速排序,5.简单选择排序,6.堆排序,7.归并排序 :");
189 cin>>t;
190 switch(t)
191 {
192 case 1:InsertSort(L);break;
193 case 2:ShellSort(L);break;
194 case 3:MPSort(L);break;
195 case 4:QuickSort(L);break;
196 case 5:SelectSort(L);break;
197 case 6:HeapSort(L);break;
198 case 7:MergeSort(L);
199 }
200 puts("排序后结果为:");
201 for(i=1;i<=L.length;i++)
202 printf("%-3d",L.r[i]);
203 putchar('\n');
204 puts("你想要退出程序吗?(Y/N)");
205 getchar();
206 if(getchar()=='N')
207 {
208 system("CLS");
209 goto loop;
210 }
211 system("pause");
212 return OK;
213 }