NSort,支持10种泛型排序的算法类

实验八:至少三种排序算法的程序实现

一、 实验目的

1.掌握简单插入排序、冒泡排序、快速排序、堆排序以及归并排序的算法并加以应用。

2.对各种查找、排序技术的时间、空间复杂性有进一步认识。

、实验要求

1.认真阅读和掌握和本实验相关的教材内容。

2.编写完整程序完成下面的实验内容并上机运行。

3.整理并上交实验报告。

三、实验内容

编写程序实现下述五种算法,并用以下无序序列加以验证:

4938659776132749

1.简单插入排序

2.冒泡排序

3.快速排序

4.归并排序

5.堆排序

四、思考与提高

       1.设有1000个无序的元素,希望用最快的速度挑出其中前10个最大的元素,采用哪一种排序方法最好?为什么?

       2.如何构造一种排序方法,使五个整数至多用七次比较就可以完成排序任务?

 ------------------------------------------------------------------------------------------------------------------------------

   1 //排序类声明文件

  2 #pragma once
  3 #include <iostream>
  4 using namespace std;
  5 
  6 class CSort
  7 {
  8 public:
  9     //直接插入排序
 10     static bool InsertSort(int* ptr,int length);
 11     //折半插入排序
 12     static bool BInsertSort(int* ptr,int length);
 13     //希尔排序
 14     static bool ShellSort(int* ptr,int length);
 15     //冒泡排序
 16     static bool BubbleSort(int* ptr,int length);
 17     //快速排序
 18     static bool QuickSort(int* ptr,int length,int zero=0);
 19     //简单选择排序
 20     static bool SelectSort(int* ptr,int length);
 21     //堆排序
 22     static bool HeapSort(int* ptr,int length);
 23     static bool HeapAdjust(int* ptr,int index,int length);  //private will be better
 24     //归并排序
 25     static bool MergeSort(int* ptr,int length);
 26     static bool MergeSort(int* ptr,int high,int low);
 27     static bool Merge(int* ptr,int low,int mid,int length);
 28     //基数排序
 29     static bool RadixSort(int* ptr,int length);
 30     //归并插入排序
 31     static bool MergeInsertSort(int* ptr,int length);
 32     //显示
 33     //template<typename T>
 34     static bool DataPrint(int* ptr,int length,bool flag);
 35     //算法信息显示
 36     static bool InfoPrint();
 37     //算法测试
 38     //template<typename T>
 39     static bool SortCheck(int* ptr,int length);
 40 };
 41 
 42 
 43 
 44 
 45 //排序类实现文件
 46 #include "Sort.h"
 47 
 48 //直接插入排序
 49 bool CSort::InsertSort(int* ptr,int length)
 50 {
 51     CSort::DataPrint(ptr,length,false);
 52     int key = 0;  // The Piquet(by ref)
 53     for (int i=1;i<length;i++)
 54     {
 55         if (*(ptr+i) < *(ptr+i-1))
 56         {
 57             key = *(ptr+i);
 58             *(ptr+i) = *(ptr+i-1);
 59             int j = 0;
 60             for (j=i-2;key < *(ptr+j);j--)
 61             {
 62                 *(ptr+j+1= *(ptr+j);
 63             }
 64             *(ptr+j+1= key;
 65         }
 66     }
 67     //Information Display
 68     CSort::InfoPrint();
 69     //Data Display
 70     CSort::DataPrint(ptr,length,true);
 71 
 72     return true;
 73 }
 74 //折半插入排序
 75 bool CSort::BInsertSort(int* ptr,int length)
 76 {
 77     CSort::DataPrint(ptr,length,false);
 78     
 79     int key =0;
 80     for (int i=1;i<length;i++)
 81     {
 82         key = *(ptr+i);
 83         int low=0,high=i-1;
 84         while(low<=high)
 85         {
 86             int m = (low+high)/2;
 87             if (key < *(ptr+m))
 88                 high = m-1;
 89             else
 90                 low = m+1;
 91         }
 92         for (int j=i-1;j>=high+1;--j)
 93             *(ptr+j+1= *(ptr+j);
 94         *(ptr+high+1= key;
 95     }
 96 
 97     //Information Display
 98     CSort::InfoPrint();
 99     //Data Display
100     CSort::DataPrint(ptr,length,true);
101 
102     return true;
103 }
104 //希尔排序
105 bool CSort::ShellSort(int* ptr,int length)
106 {
107     CSort::DataPrint(ptr,length,false);
108     
109     int dlta[3= {5,3,1};
110     for (int k=0;k<3;++k)
111     {
112         int dk = dlta[k];
113         int key =0;
114         for (int i=dk;i<length;++i)//i<2*dk..?
115         {
116             if (*(ptr+i) < *(ptr+i-dk))
117             {
118                 key = *(ptr+i);
119                 int j=0;
120                 for (j=i-dk;j>=0&&key<*(ptr+j);j-=dk)
121                     *(ptr+j+dk) = *(ptr+j);
122                 *(ptr+j+dk) = key;
123             }
124         }
125     }
126 
127     //Information Display
128     CSort::InfoPrint();
129     //Data Display
130     CSort::DataPrint(ptr,length,true);
131 
132     return true;
133 }
134 
135 //冒泡排序
136 bool CSort::BubbleSort(int* ptr,int length)
137 {
138     CSort::DataPrint(ptr,length,false);
139 
140     bool flag = true;
141     for (int i=length-1;flag&&i>0;--i)//i>0 or i>=0..?
142     {
143         flag = false;
144         for (int j=0;j<i;++j)
145         {
146             int key = 0;
147             if ( *(ptr+j) > *(ptr+j+1) )
148             {
149                 key = *(ptr+j);
150                 *(ptr+j) = *(ptr+j+1);
151                 *(ptr+j+1= key;
152                 flag = true;
153             }
154         }
155     }
156 
157 
158     //Information Display
159     CSort::InfoPrint();
160     //Data Display
161     CSort::DataPrint(ptr,length,true);
162 
163     return true;
164 }
165 
166 //快速排序
167 bool CSort::QuickSort(int* ptr,int length,int zero)
168 {
169     int high = length-1,low = zero;
170     if (low<high)
171     {
172         int pivotkey = *(ptr+low);
173         while(low<high)
174         {
175             while(low<high&&*(ptr+high)>=pivotkey)
176                 high--;
177             *(ptr+low) = *(ptr+high);
178             
179             while(low<high&&*(ptr+low)<=pivotkey)
180                 low++;
181             *(ptr+high) = *(ptr+low);
182         }
183         *(ptr+low) = pivotkey;
184         QuickSort(ptr,low,zero);
185         QuickSort(ptr,length,low+1);
186     }
187     return true;
188 }
189 
190 //简单选择排序
191 bool CSort::SelectSort(int* ptr,int length)
192 {
193     CSort::DataPrint(ptr,length,false);
194 
195     for (int i=0;i<length;i++)
196     {
197         int flag = i;
198         for (int j=i+1;j<length;j++)
199         {
200             if (*(ptr+flag) > *(ptr+j))
201             {
202                 flag = j;
203             }
204         }
205         if (flag!=i)
206         {
207             int temp = *(ptr+i);
208             *(ptr+i) = *(ptr+flag);
209             *(ptr+flag) = temp;
210         }
211     }
212 
213     //Information Display
214     CSort::InfoPrint();
215     //Data Display
216     CSort::DataPrint(ptr,length,true);
217 
218     return true;
219 }
220 
221 
222 bool CSort::InfoPrint()
223 {
224     //switch case,可以确定目前调用的是哪个排序算法,然后显示时间复杂度和一些其它信息..
225     //暂未完成..。
226     cout<<"Sort OK\n";
227     return true;
228 }
229 
230 
231 //堆排序
232 bool CSort::HeapSort(int* ptr,int length)
233 {
234     for (int i=length/2;i>=0;--i)
235         HeapAdjust(ptr,i,length-1);
236     for (int i=length-1;i>0;--i)
237     {
238         {        
239             int temp = *ptr;
240             *ptr = *(ptr+i);
241             *(ptr+i) = temp;
242         }
243         HeapAdjust(ptr,0,i-1);
244     }
245 
246     //Information Display
247     CSort::InfoPrint();
248     //Data Display
249     CSort::DataPrint(ptr,length,true);
250 
251     return true;
252 }
253 //堆调整,建立大顶堆
254 bool CSort::HeapAdjust(int* ptr,int index,int length)
255 {
256     int key = *(ptr+index);
257     for (int i= 2*index;i<=length;i*=2)
258     {
259         if (i<length&& *(ptr+i)<*(ptr+i+1))
260             ++i;
261         if (key >= *(ptr+i))
262             break;
263         *(ptr+index) = *(ptr+i);
264         index = i;
265     }
266     *(ptr+index) = key;
267 
268     return true;
269 }
270 
271 //归并排序
272 //归并排序
273 bool CSort::MergeSort(int* ptr,int length)
274 {
275     MergeSort(ptr,--length,0);
276     return true;
277 }
278 bool CSort::MergeSort(int* ptr,int high,int low)
279 {
280     if(low < high)
281     {
282         int mid =(low + high)/2;
283         MergeSort(ptr,mid,low);
284         MergeSort(ptr,high,mid+1);
285         Merge(ptr,low,mid,high);
286     }
287 
288     return true;
289 }
290 //合并
291 bool CSort::Merge(int* ptr,int low,int mid,int length)
292 {
293     int *p1 = new int[mid-low+1];
294     int *p2 = new int[length-mid];
295     for (int i=0;i<mid-low+1;i++)
296     {
297         *(p1+i) = *(ptr+low+i);
298     }
299     for (int i=0;i<length-mid;i++)
300     {
301         *(p2+i) = *(ptr+mid+i+1);
302     }
303 
304     int i=0,j=0,k= 0;
305     for (k=low;i<mid-low+1&&j<length-mid;k++)
306     {
307         if (p1[i] < p2[j])
308             *(ptr+k) = p1[i++];
309         else
310             *(ptr+k) = p2[j++];
311     }
312     while (i<mid-low+1)
313     {
314         *(ptr+k) = p1[i++];
315         k++;
316     }
317     while (j<length-mid)
318     {
319         *(ptr+k) = p2[j++];
320         k++;
321     }
322 
323     free(p1);
324     free(p2);
325 
326     return true;
327 }
328 
329 //display the sorted data
330     //template <typename T>
331 bool CSort::DataPrint(int* ptr,int length,bool flag)
332 {
333     flag ? cout<<"The Sorted Data\n" : cout<<"The UnSorted Data\n";
334 
335     for (int i=0;i<length;i++)
336     {
337         cout<<ptr[i]<<"\t";
338     }
339     cout<<endl;
340     
341     return true;
342 }
343 
344 //算法测试
345     //template <typename T>
346 bool CSort::SortCheck(int* ptr,int length)
347 {
348     int i = 0;
349     for (i=0;i<length-1;i++)
350     {
351         if (ptr[i] > ptr[i+1])
352             break;
353     }
354     return i==length-1 ? true : false ;
355 };
356 
357 
358 
359 
360 //测试入口点
361 #include "Sort.h"
362 
363 /*//////////////////////////
364 // Author: NewSketcher
365 // E-mail: 0x0o@Live.Cn
366 //REM:  递归排序算法均没有设置为自动显示排序结果..
367 //REM:  没大规模数据测试,个别算法应该还是有问题..
368 *///////////////////////////
369 int main()
370 {
371     int ary[8= {49,38,65,97,76,13,27,49};
372     //CSort::InsertSort(ary,8);
373     //CSort::BInsertSort(ary,8);
374     //CSort::ShellSort(ary,8);
375     //CSort::BubbleSort(ary,8);
376     CSort::QuickSort(ary,8);
377     CSort::DataPrint(ary,8,true);
378     //CSort::SelectSort(ary,8);
379     //CSort::HeapSort(ary,8);
380     //CSort::MergeSort(ary,8);
381     //CSort::DataPrint(ary,8,true);
382     if (!CSort::SortCheck(ary,8))
383     {
384         cout<<"Sort Error!"<<endl;
385     }
386 
387     return 0;
388 }
389 
390 
391 


代码下载:NSort.rar

 

My Solution  

在面试时您是不是想不起冒泡排序为何物?

在数据结构作业来临时您是不是无所适从?

在项目开发中您是不是找不出合适的排序辅助类?

在非数值类排序中您是不是找不到相关类型排序类?

在研究算法时您是不是为各种排序方法以及其时间复杂度计算而苦恼?


NSort为您带来一套完美的解决方案,虽然目前的代码仅仅是一个beta版,但是:

不久的将来,我们会以此为蓝图,完成一个支持10种以上泛型排序算法的类库:

1、加入归并插入排序算法

2、支持template,支持泛型,支持自定义比较

3、为用户提供一致的接口,屏蔽各种递归排序的可恶参数

4、完成InfoPrint,自动根据被调用的排序方法显示其相关特性,例如时间复杂度等

5、开源、免费

6、...

--------------------------------------------------------------------------------------------

..以上部分纯属恶搞..如有雷同,大家一起恶搞..

 

 

 

 

        NewSketcher

                                                08.12.28  0058

posted @ 2008-12-28 01:10  端木  阅读(1136)  评论(0编辑  收藏  举报