常用排序算法

  1 #include <iostream>
  2 #include <cassert>
  3 
  4 using namespace std;
  5 
  6 template <typename T>
  7 void printArray(T array[],int arrayLen)
  8 {
  9     if (arrayLen > 1)
 10     {
 11         cout<<endl<<"\t\t";
 12         for (int i=0;i<arrayLen;i++)
 13         {
 14             cout<<array[i]<<" ";
 15         }
 16         cout<<endl;
 17     }
 18     return;
 19 }
 20 
 21 template <typename T>
 22 void randomPerm(T array[],int arrayLen)
 23 {
 24     if (arrayLen > 1)
 25     {
 26         T ex;
 27         int rand1,rand2;
 28         for (int i=0;i<arrayLen; i++)
 29         {
 30             rand1=rand()%arrayLen;
 31             rand2=rand()%arrayLen;
 32             if (rand2!=rand1)
 33             {
 34                 ex=array[rand1];
 35                 array[rand1]=array[rand2];
 36                 array[rand2]=ex;        
 37             }
 38         }
 39     }
 40     return;
 41 }
 42 
 43 template <typename T>
 44 void selectSort(T array[],int arrayLen)
 45 {
 46     if (arrayLen > 1)
 47     {
 48         T ex;
 49         int t;
 50         for (int i=1;i<arrayLen-1;i++)
 51         {
 52             t=i-1;
 53             for (int j=i;j<arrayLen;j++)
 54             {
 55                 if (array[j]<array[t])
 56                 {
 57                     t=j;
 58                 }
 59             }
 60             if (t==i-1)
 61             {
 62                 continue;
 63             }
 64             ex=array[t];
 65             array[t]=array[i-1];
 66             array[i-1]=ex;            
 67             
 68         }
 69     }
 70     return;
 71 }
 72 
 73 template <typename T>
 74 void bubbleSort(T array[],int arrayLen)
 75 {
 76     if (arrayLen > 1)
 77     {
 78         bool flag=true;
 79         T ex;
 80         for (int i=0;i<arrayLen-1 && flag;i++)
 81         {
 82             flag=false;
 83             for (int j=0;j<arrayLen-i-1;j++)
 84             {
 85                 if (array[j]>array[j+1])
 86                 {
 87                     ex=array[j];
 88                     array[j]=array[j+1];
 89                     array[j+1]=ex;    
 90                     flag=true;
 91                 }
 92             }
 93         }
 94     }
 95     return;
 96 }
 97 
 98 template <typename T>
 99 void insertSort(T array[],int arrayLen)
100 {
101     if (arrayLen > 1)
102     {
103         T ex;
104         bool flag=true;
105         for (int i=0;i<arrayLen-1&&flag;i++)
106         {
107             flag=false;
108             for (int j=i+1;j>0;j--)
109             {
110                 if (array[j]<array[j-1])
111                 {
112                     ex=array[j];
113                     array[j]=array[j-1];
114                     array[j-1]=ex;    
115                     flag=true;
116                 }
117             }
118         }
119     }
120     return;
121 }
122 
123 template <typename T>
124 void heapSort(T array[],int arrayLen,bool max)
125 {
126     if (arrayLen > 1)
127     {
128         T ex;
129         void (*heap)(T array[],int arrayLen);
130         heap=minHeap;
131         if (max)
132         {
133             heap=maxHeap;
134         }
135         heap(array,arrayLen);
136         for (int i=0;i<arrayLen-1;i++)
137         {
138             ex=array[arrayLen-i-1];
139             array[arrayLen-i-1]=array[0];
140             array[0]=ex;
141             heap(array,arrayLen-i-1);
142         }
143     }
144     return;
145 }
146 
147 template <typename T>
148 void  minHeap(T array[],int arrayLen)
149 {
150     if (arrayLen > 1)
151     {    
152         T ex;
153         for (int i=arrayLen/2-1;i>=0;i--)
154         {
155             if ((2*i+1)<arrayLen)
156             {
157                 if (array[i]>array[2*i+1])
158                 {
159                     ex=array[i];
160                     array[i]=array[2*i+1];
161                     array[2*i+1]=ex;
162                 }
163             }
164             if ((2*i+2)<arrayLen)
165             {
166                 if (array[i]>array[2*i+2])
167                 {
168                     ex=array[i];
169                     array[i]=array[2*i+2];
170                     array[2*i+2]=ex;
171                 }
172             }
173         }
174     }
175     return;
176 }
177 
178 template <typename T>
179 void  maxHeap(T array[],int arrayLen)
180 {
181     if (arrayLen > 1)
182     {
183         T ex;
184         for (int i=arrayLen/2-1;i>=0;i--)
185         {
186             if ((2*i+1)<arrayLen)
187             {
188                 if (array[i]<array[2*i+1])
189                 {
190                     ex=array[i];
191                     array[i]=array[2*i+1];
192                     array[2*i+1]=ex;
193                 }
194             }
195             if ((2*i+2)<arrayLen)
196             {
197                 if (array[i]<array[2*i+2])
198                 {
199                     ex=array[i];
200                     array[i]=array[2*i+2];
201                     array[2*i+2]=ex;
202                 }
203             }
204         }
205     }
206     return;
207 }
208 
209 template <typename T>
210 void quikSort(T array[],int start,int end)
211 {
212     if (start < end)
213     {    
214         T key=array[end];
215         int i=start-1,j=start;
216         T ex;
217         while (j<=end-1)
218         {
219             if (array[j]<key)
220             {
221                 i++;
222                 ex=array[i];
223                 array[i]=array[j];
224                 array[j]=ex;
225             }
226             j++;
227         }
228         ex=array[i+1];
229         array[i+1]=array[end];
230         array[end]=ex;
231         quikSort(array,start,i);
232         quikSort(array,i+2,end);
233         }
234     return;
235 }
236 
237 template <typename T>
238 void mergeSort(T array[],int start,int end)
239 {
240     if (start < end)
241     {
242         int mid=(start+end)/2;
243         mergeSort(array,start,mid);
244         mergeSort(array,mid+1,end);
245         insertSort(&array[start],end-start+1);
246         //selectSort(&array[start],end-start+1);
247         //printArray(array,10);
248     }    
249 }
250 
251 template <typename T>
252 void shellSort(T array[],int arrayLen,int seed)
253 {
254     if (seed && arrayLen > 1)
255     {
256         T ex;
257         bool flag=true;
258         for (int i=0;i<arrayLen-seed && flag;i++)
259         {
260             flag=false;
261             for (int j=i+seed;j>0;j-=seed)
262             {
263                 if (array[j]<array[j-seed])
264                 {
265                     ex=array[j];
266                     array[j]=array[j-seed];
267                     array[j-seed]=ex;
268                     flag=true;
269                 }
270             }
271         }
272         seed/=2;
273         //printArray(array,10);
274         shellSort(array,arrayLen,seed);
275     }
276 }
277 void main(void)
278 {
279     double array[10]={0.3,3.2,5.7,2.1,4.5,1.9,8.6,7.3,9.4,6.1};
280     
281     cout<<"Select Sort"<<endl;
282     randomPerm(array,10);
283     cout<<"Original array: ";
284     printArray(array,10);
285     selectSort(array,10);
286     cout<<"Sorted array: ";
287     printArray(array,10);
288     cout<<endl;
289     
290     cout<<"Bubble Sort"<<endl;
291     randomPerm(array,10);
292     cout<<"Original array: ";
293     printArray(array,10);
294     bubbleSort(array,10);
295     cout<<"Sorted array: ";
296     printArray(array,10);
297     cout<<endl;
298 
299     cout<<"Insert Sort"<<endl;
300     randomPerm(array,10);
301     cout<<"Original array: ";
302     printArray(array,10);
303     //insertSort(array,10);
304     shellSort(array,10,1);
305     cout<<"Sorted array: ";
306     printArray(array,10);
307     cout<<endl;
308 
309     cout<<"Heap Sort"<<endl;
310     randomPerm(array,10);
311     cout<<"Original array: ";
312     printArray(array,10);
313     heapSort(array,10,true);
314     cout<<"Sorted array: ";
315     printArray(array,10);
316     cout<<endl;
317 
318     cout<<"Quik Sort"<<endl;
319     randomPerm(array,10);
320     cout<<"Original array: ";
321     printArray(array,10);
322     quikSort(array,0,9);
323     cout<<"Sorted array: ";
324     printArray(array,10);
325     cout<<endl;
326 
327     cout<<"Merge Sort"<<endl;
328     randomPerm(array,10);
329     cout<<"Original array: ";
330     printArray(array,10);
331     mergeSort(array,0,9);
332     cout<<"Sorted array: ";
333     printArray(array,10);
334     cout<<endl;
335 
336     cout<<"Shell Sort"<<endl;
337     randomPerm(array,10);
338     cout<<"Original array: ";
339     printArray(array,10);
340     shellSort(array,10,5);
341     cout<<"Sorted array: ";
342     printArray(array,10);
343     cout<<endl;
344 
345     printf("%d", sizeof(unsigned short int));
346     system("pause");
347     return;
348 }

 

 

 

posted @ 2013-08-31 22:08  alpha19881007  阅读(195)  评论(0编辑  收藏  举报