【数据结构】排序算法:希尔、归并、快速、堆排序

排序算法


排序是非常常用,非常基本的算法。排序的方法有很多,比如插入排序、选择排序、希尔排序、归并排序、快速排序、堆排序。
本次试验重点实现:希尔排序、归并排序、快速排序、堆排序

插入排序

简单说就是每次选未排序的队列中最小的条目插入到已排序队列的最后:



选择排序

选择排序和插入有点像,是每次从拿未排序中的第一个条目插入到已排序中应该插入的位置:



希尔排序

希尔排序是插入排序的一种,是针对直接插入排序算法的改进。
希尔排序的基本思想是:先取一个小于count的增量increment,把表中Record分为increment组,所有距离为increment的Record为同一组,现在各组中进行直接插入排序(insert_sort),然后减小increment重复分组和排序,知道increment=1,即所有Record放在同一组中进行直接插入排序为止。



【相关实验】

首先从类表List中派生子表Sortable_list。为了方便定义,我们可以重载这样的构造函数
  1. template<class Record>  
  2. Sortable_list<Record>::Sortable_list(const Record A[],int size)  
  3. {  
  4.     for(int i=0;i<size;i++)  
  5.         insert(i,A[i]);  
  6. }  
1.编写函数shell_sort。函数中我们从首先定义increment=0,观察题目要求,可以得到在循环中有这样的关系increment=increment/2;使用循环将每次分组后的每组排列,排列我们再增加函数sort_interval();在每组的排序中使用的直接插入排序,所以可以这样实现sort_interval:每次定义一个临时的Sortable_list对象tmp记录每次分组的小组,对tmp使用insertion_sort,进而我们编写函数insertion_sort();
2.实现表的排序一个重要的步骤是将Record换到相应位置,即交换,所以编写函数swap;
3.为了输出每趟排序结果,我们再编写一个全局函数print_out,由List的成员函数traverse()调用;调用的过程在置于swap之中,即每次有交换(或看做移动)则看做一趟排序。
相应算法函数如下:
  1. template<class Record>  
  2. void Sortable_list<Record>::shell_sort()  
  3. /*Post: The entries of the Sortable_list have been rearranged so that the keys in  
  4.         all the entries are sorted into nondecreasing order. 
  5.   Uses: sort_interval. 
  6. */  
  7. {  
  8.     int increment,   //spacing of entries in sublist  
  9.         start;       //starting point of sublist  
  10.     increment=count;  
  11.     do{  
  12.         increment=increment/2;  
  13.         for(start=0;start<increment;start++){  
  14.             sort_interval(start,increment);  //modified insertion sort  
  15.             traverse(print_out);  
  16.             cout<<endl;  
  17.         }  
  18.     }while(increment>1);  
  19. }  
  20.   
  21. template<class Record>  
  22. void Sortable_list<Record>::sort_interval(int start,int increment)  
  23. {  
  24.     Sortable_list<Record> temp;  
  25.     int j=0;  
  26.     for(int i=start;i<size();i=i+increment){  
  27.         Record temp_record;  
  28.         retrieve(i,temp_record);  
  29.         temp.insert(j,temp_record);  
  30.         j++;  
  31.     }  
  32.     temp.insertion_sort();  
  33.     j=0;  
  34.     for(int k=start;k<size();k+=increment){  
  35.         Record temp_record;  
  36.         temp.retrieve(j,temp_record);  
  37.         replace(k,temp_record);  
  38.         j++;  
  39.     }  
  40. }  
  41. template<class Record>  
  42. void Sortable_list<Record>::insertion_sort()  
  43. /*Post: The entries of the Sortable_list have been rearranged so that the keys in 
  44.         all the entries are sorted into nondecreasing order. 
  45.   Uses: Methods for the class Record; the contiguous List  
  46. */  
  47. {  
  48.     int first_unsorted;            //position of first unsorted entry  
  49.     int position;                  //searches sorted part of list  
  50.     Record current;                //holds the entry emporarily removed from list  
  51.     for(first_unsorted=1;first_unsorted<count;first_unsorted++)  
  52.         if(entry[first_unsorted]<entry[first_unsorted-1]){  
  53.             position=first_unsorted;  
  54.             current=entry[first_unsorted]; //Pull unsorted entry out of the list.  
  55.             do{                    //Shift all entries until the proper position is found  
  56.                 entry[position]=entry[position-1];  
  57.                 position--;         //position if empty.  
  58.             }while(position>0&&entry[position-1]>current);  
  59.             entry[position]=current;  
  60.         }  
  61. }  
  62. //其他辅助函数见源文件  

【实验结果】




归并排序

归并排序是采用分治的思想将两个已排序的表合并成一个表
归并排序算法的基本思想是:先将一个表分成两个表(当个数是奇数时,使左表的元素比右表多一)。对两个表分别进行归并排序,然后将两个已排序好的表合并。合并的思路就像将两罗纸牌混成一摞,每次取顶上最小的纸牌。



【相关实验】


1.仍旧使用上述的Sortable_list
2.根据归并排序的思想,每次子表仍旧使用归并排序,可以通过递归实现。所以编写递归函数recursive_merge_sort(),要把已排序好的子表合并,所以编写合并子表的辅助函数merge()
3.为了输出每趟排序结果,在归并时merge中加入traverse(print_out)  //但因为递归调用的问题,很多次我们还是看不到过程
  1. template<class Record>  
  2. void Sortable_list<Record>::merge(int low,int high)  
  3. {  
  4.     Record *tmp=new Record[high-low+1];  
  5.     int index=0;  
  6.     int index1=low,mid=(low+high)/2,index2=mid+1;  
  7.     while(index1<=mid&&index2<=high){  
  8.         if(entry[index1]<entry[index2])  
  9.             tmp[index++]=entry[index1++];  
  10.         else  
  11.             tmp[index++]=entry[index2++];  
  12.     }  
  13.     while(index1<=mid)  
  14.         tmp[index++]=entry[index1++];  
  15.     while(index2<=high)  
  16.         tmp[index++]=entry[index2++];  
  17.     for(index=low;index<=high;index++)  
  18.         entry[index]=tmp[index-low];  
  19.     delete []tmp;  
  20.     traverse(print_out);  
  21.     cout<<endl;  
  22.       
  23. }  
  24.   
  25. template<class Record>  
  26. void Sortable_list<Record>::recursive_merge_sort(int low,int high)  
  27. /*Post: The entries of the sortable list between 
  28.         index low and high have been rearranged so that 
  29.         their keys are sorted into nondecreasing order. 
  30.   Uses: The contiguous list 
  31. */  
  32. {  
  33.     if(high>low){  
  34.         recursive_merge_sort(low,(high+low)/2);  
  35.         recursive_merge_sort((high+low)/2+1,high);  
  36.         merge(low,high);  
  37.     }  
  38. }  
  39.   
  40. template<class Record>  
  41. void Sortable_list<Record>::merge_sort()  
  42. /* Post: The entries of the sortable list have been rearranged so that 
  43.          their keys are sorted into nondecreasing order. 
  44.    Uses: The contiguous list 
  45. */  
  46. {  
  47.     recursive_merge_sort(0,size()-1);  
  48. }  

【实验结果】



快速排序

快速排序是对冒泡排序的一种改进。
快速排序算法的基本思想是:每一趟排序中找一个点pivot,将表分割成独立的两部分,其中一部分的所有Record都比pivot小,另一部分比pivot大,然后再按此方法对这两部分数据分别进行快速排序。 



【相关实验】

1.仍旧使用上述的Sortable_list。
2.根据快速排序的思想,每趟排序将表分成两部分然后仍旧进行快速排序,所以可以通过递归实现,而为了调用递归函数,我们首先编写给定要排序范围的参数的函数recursive_quick_sort(int low,int high)//之所以不将开始的quick_sort直接写作递归函数,是为了避免输入参数而方便用户调用。另外需要一个partition函数,返回每趟排序之后的分点。
3.为了输出每趟排序,我的想法是在每次递归中使用traverse(print_out)输出,但却不是想象的结果。因为递归是每次递归出来之后才执行函数print_out,除了前几次可以看到结构,后来都是在排序好之后…所以我们仍旧通过swap函数实现输出。
  1. template<class Record>  
  2. int Sortable_list<Record>::partition(int low,int high)  
  3. /*Pre:  low and high are valid positions of the Sortable_list, with low<=high. 
  4.   Post: The center (or left center) entry in the range between indices low and  
  5.         high of the Sortable_list has been chosen as a pivot. All entries of the 
  6.         Sortable_list between indices low and high, inclusive, have been rearranged 
  7.         so that chosen with keys less than the pivot com before the pivot 
  8.         and the remaining entries come after the pivot. The final position of the  
  9.         pivot is returned. 
  10.  Uses: swap(int,int j) contigious list 
  11. */  
  12. {  
  13.     Record pivot;  
  14.     int i,           //used to scan through the list  
  15.         last_small;  //position of the last key less than pivot  
  16.     swap(low,(low+high)/2);  
  17.     pivot=entry[low];  
  18.     last_small=low;  
  19.     for(i=low+1;i<=high;i++)  
  20.     //At the beginning of each iteration of this loop, we have the following conditions:  
  21.     //    If low<j<=last_samll then entry[j].key<pivot  
  22.     //    If last_small<j<i then entry[j].key>=pivot.  
  23.        if(entry[i]<pivot){  
  24.            last_small=last_small+1;  
  25.            swap(last_small,i);  //Move large entry to right and small to left  
  26.        }  
  27.     swap(low,last_small);   //Put the pivot into its proper position.  
  28.     return last_small;  
  29. }  
  30.   
  31. template<class Record>  
  32. void Sortable_list<Record>::recursive_quick_sort(int low,int high)  
  33. /*Pre:  low and high are valid positions in the Sortable list. 
  34.   Post: The entries of the Sortable_list have been rearranged so that their keys 
  35.         are sorted into nondecreasing order. 
  36.   Uses: The contiguous list, recursive_quick_sort, partition. 
  37. */  
  38. {  
  39.     int pivot_postion;  
  40.     if(low<high){  
  41.         pivot_postion=partition(low,high);  
  42.         recursive_quick_sort(low,pivot_postion-1);  
  43.         recursive_quick_sort(pivot_postion+1,high);  
  44.     }  
  45. }  
  46.   
  47. template<class Record>  
  48. void Sortable_list<Record>::quick_sort()  
  49. {  
  50.     recursive_quick_sort(0,count-1);  
  51. }  
  52. //其他函数见源码  

【实验结果】



堆排序

堆排序是先将表中Record按大堆(或小堆)存放,使选取最大的Record变的极为容易,每次选取之后再提升堆。实现排序。


继续:



【相关实验】

1.仍旧使用上述的Sortable_list。
2.编写heap_sort()函数。按思路,首先应该建堆,然后取堆顶元素,之后对剩下元素重新建堆(提升堆),所以我们需要编写build_heap()函数,其通过inster_heap函数将元素一个个插入堆中。
最后实现heap_sort函数。
3.我们在每次插入堆时调用traverse(print_sort)实现对每趟排序的输出。
  1. template<class Record>  
  2. void Sortable_list<Record>::insert_heap(const Record ¤t,int low,int high)  
  3. /*Pre:  The entries of the Sortable_list between indices low+1 and high, 
  4.         inclusive,form a heap. The entry in position low will be discarded. 
  5.   Post: The entry current has been inserted into the Sortabl_list and the entries, 
  6.         rearranged so that the entries between indices low and high, inclusive 
  7.         form a heap. 
  8. */  
  9. {  
  10.     int large;       //position of child of entry[low] with the larger key  
  11.     large=2*low+1;   //large is now the left child of low  
  12.     while(large<=high){  
  13.         if(large<high&&entry[large]<entry[large+1])  
  14.             large++;    //large is now the child of low with the largest key.  
  15.         if(current>=entry[large])  
  16.             break;      //current belongs in position low  
  17.         else{           //Promote entry[large] and move down the tree   
  18.             entry[low]=entry[large];  
  19.             low=large;  
  20.             large=2*low+1;  
  21.         }  
  22.     }  
  23.     entry[low]=current;  
  24.     traverse(print_out);  
  25.     cout<<endl;  
  26. }  
  27.   
  28. template<class Record>  
  29. void Sortable_list<Record>::build_heap()  
  30. /*Post: The entries of the Sortable_list have been rearranged so that  
  31.         it becomes a heap 
  32.   Uses: The contiguous list and insert_heap 
  33. */  
  34. {  
  35.     int low;  
  36.     for(low=count/2-1;low>=0;low--){  
  37.         Record current=entry[low];  
  38.         insert_heap(current,low,count-1);  
  39.     }  
  40. }  
  41.   
  42. template<class Record>  
  43. void Sortable_list<Record>::heap_sort()  
  44. /*Post: The entries of the Sortable_list have been rearranged so that their keys 
  45.         are sorted into nondecreasing order. 
  46.   Uses: The contiguous list, build_heap, insert_heap 
  47. */  
  48. {  
  49.     Record current;       //temporary storage for moving entries  
  50.     int last_unsorted;    //Entries beyond last_unsorted have been sorted.  
  51.     build_heap();         //First phase: Turn the list into a heap  
  52.     for(last_unsorted=count-1;last_unsorted>0;last_unsorted--){  
  53.         current=entry[last_unsorted];   //Extract the last entry from the list  
  54.         entry[last_unsorted]=entry[0];  //Move top of heap to the end  
  55.         insert_heap(current,last_unsorted-1);  //Restore the heap  
  56.     }  
  57. }  
  58. //其他函数见源码  

【实验结果】



结果分析

【希尔排序】

1.希尔排序是直接插入的一种改进,在效率上较直接插入排序有较大的改进。
直接插入排序每次只能将Record移动一个位置,即增量increment为1,而希尔排序开始时增量较大,分组较多,每组Record数目少,故各组内直接插入较快;increment递减之后分组逐渐减少,Record增多,但由于已经在increment较大时进行过排序,表更接近于有序状态,新一趟的排序也较快。
2.我在实验中子表的排序是通过定义一个新表,然后调用直接插入函数。但这种方法效率并不高,而且浪费空间;直接对子表进行直接插入的排序是一种更好的方法。
3.希尔排序复杂度为:O(nlog2n)   d =1希尔与直接插入排序基本一致
4.希尔排序是不稳定的排序算法,即相等元素的顺序可能改变

【归并排序】

1.归并排序在实现上用链式表更为合理,因为合并中需要定义新的表,即使我们通过动态定义再删除,以节省不必要的空间,这些工作仍是有些费时。而链式表只是返回指针,对节点Node的Node<Node_entry>*next部分进行操作,不需要数据的移动。但同时链式表的使用也需要对指针有熟悉的掌握,很容易出错,先画图再编码往往会有更清晰的思路。
2.归并排序的复杂度为:O(nlog2n)
3.归并排序是稳定的排序算法,即相等元素的顺序不会改变

【快速排序】

1.复杂度:最好 O(nlog2n),最坏 O(n2)
2.快速排序的最坏情况基于每次划分对主元的选择。基本的快速排序选取第一个元素作为主元。这样在数组已经有序的情况下,每次划分将得到最坏的结果。一种比较常见的优化方法是随机化算法,即随机选取一个元素作为主元。这种情况下虽然最坏情况仍然是O(n^2),但最坏情况不再依赖于输入数据,而是由于随机函数取值不佳。
3.快速排序是一种不稳定的排序算法

【堆排序】

1.实现堆排序很重要的一个操作就是建堆
要将初始表调整为一个大根堆,就必须将它所对应的完全二叉树中以每一结点为根的子树都调整为堆。显然只有一个结点的树是堆,而在完全二叉树中,所有序号大于n/2的结点都是叶子,因此以这些结点为根的子树均已是堆。这样,我们只需依次将以序号为n/2,…,1的结点作为根的子树都调整为堆即可。
2.堆[排序的时间,主要由建立初始堆和反复重建堆这两部分的时间开销构成,堆排序的最坏时间复杂度为O(nlog2n)。
3.堆排序是不稳定的排序算法
4.由于建初始堆所需的比较次数较多,所以堆排序不适宜于记录数较少的文件。
堆排序每次在大堆中直接选择出顶即最大的元素,这与选择排序极为相似,但他们是不同的,堆排序的性能更为优越。因为选择排序中,为了从表中选出最小的记录,必须进行n-1次比较,然后在剩余表中选出关键字最小的记录,又需要做n-2次比较。事实上,后面的n-2次比较中,有许多比较可能在前面的n-1次比较中已经做过,但由于前一趟排序时未保留这些比较结果,所以后一趟排序时又重复执行了这些比较操作。堆排序可通过树形结构保存部分比较结果,可减少比较次数。

原文地址:http://www.voidcn.com/article/p-ucctzjhq-vo.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


【啊哈!算法】算法3:最常用的排序——快速排序       上一节的冒泡排序可以说是我们学习第一个真正的排序算法,并且解决了桶排序浪费空间的问题,但在算法的执行效率上却牺牲了很多,它的时间复杂度达到了O(N2)。假如我们的计算机每秒钟可以运行10亿次,那么对1亿个数进行排序,桶排序则只需要0.1秒,而冒泡排序则需要1千万秒,达到115天之久,是不是很吓人。那有没有既不浪费空间又可以快一点的排序算法
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正则表达式就可以组成第一个构造。正如稍后要介绍的一样,既然也可以命名组,大家就有考虑把这个构造作为匿名组。作为一个实例,请看看下列字符串: “08/14/57 46 02/25/59 45 06/05/85 18 03/12/88 16 09/09/90 13“ 这个字符串就是由生日和年龄组成的。如果需要匹配年两而不要生日,就可以把正则
选择排序:从数组的起始位置处开始,把第一个元素与数组中其他元素进行比较。然后,将最小的元素方式在第0个位置上,接着再从第1个位置开始再次进行排序操作。这种操作一直到除最后一个元素外的每一个元素都作为新循环的起始点操作过后才终止。 public void SelectionSort() { int min, temp;
public struct Pqitem { public int priority; public string name; } class CQueue { private ArrayList pqueue; public CQueue() { pqueue
在编写正则表达式的时候,经常会向要向正则表达式添加数量型数据,诸如”精确匹配两次”或者”匹配一次或多次”。利用数量词就可以把这些数据添加到正则表达式里面了。 数量词(+):这个数量词说明正则表达式应该匹配一个或多个紧紧接其前的字符。 string[] words = new string[] { "bad", "boy", "baad", "baaad" ,"bear", "b
来自:http://blog.csdn.net/morewindows/article/details/6678165/归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。首先考虑下如何将将二个有序数列合并。这个非常简单,只要从比较二个数列的第一个数,谁小就先取谁,取了后就在对应数列中删除这个数。然后再进行比较,如果有数列
插入排序算法有两层循环。外层循环会啄个遍历数组元素,而内存循环则会把外层循环所选择的元素与该元素在数组内的下一个元素进行比较。如果外层循环选择的元素小于内存循环选择的元素,那么瘦元素都想右移动以便为内存循环元素留出位置。 public void InsertionSort() { int inner, temp;
public int binSearch(int value) { int upperBround, lowerBound, mid; upperBround = arr.Length - 1; lowerBound = 0; while (lowerBound <= upper
虽然从表内第一个节点到最后一个节点的遍历操作是非常简单的,但是反向遍历链表却不是一件容易的事情。如果为Node类添加一个字段来存储指向前一个节点的连接,那么久会使得这个反向操作过程变得容易许多。当向链表插入节点的时候,为了吧数据复制给新的字段会需要执行更多的操作,但是当腰吧节点从表移除的时候就能看到他的改进效果了。 首先需要修改Node类来为累增加一个额外的链接。为了区别两个连接,这个把指
八、树(Tree)树,顾名思义,长得像一棵树,不过通常我们画成一棵倒过来的树,根在上,叶在下。不说那么多了,图一看就懂:当然了,引入了树之后,就不得不引入树的一些概念,这些概念我照样尽量用图,谁会记那么多文字?树这种结构还可以表示成下面这种方式,可见树用来描述包含关系是很不错的,但这种包含关系不得出现交叉重叠区域,否则就不能用树描述了,看图:面试的时候我们经常被考到的是一种叫“二叉树”的结构,二叉
Queue的实现: 就像Stack类的实现所做的一样,Queue类的实现用ArrayList简直是毋庸置疑的。对于这些数据结构类型而言,由于他们都是动态内置的结构,所以ArrayList是极好的实现选择。当需要往队列中插入数据项时,ArrayList会在表中把每一个保留的数据项向前移动一个元素。 class CQueue { private ArrayLis
来自:http://yingyingol.iteye.com/blog/13348911 快速排序介绍:快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要Ο(n log n)次比较。在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见。事实上,快速排序通常明显比其他Ο(n log n) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地
Stack的实现必须采用一种基本结构来保存数据。因为再新数据项进栈的时候不需要担心调整表的大小,所以选择用arrayList.using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collecti
数组类测试环境与排序算法using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Data_structure_and_algorithm{ class CArray { pr
一、构造二叉树 二叉树查找树由节点组成,所以需要有个Node类,这个类类似于链表实现中用到的Node类。首先一起来看看Node类的代码。 public class Node { public int Data; public Node Left; public Node Right; public v
二叉树是一种特殊的树。二叉树的特点是每个结点最多有两个儿子,左边的叫做左儿子,右边的叫做右儿子,或者说每个结点最多有两棵子树。更加严格的递归定义是:二叉树要么为空,要么由根结点、左子树和右子树组成,而左子树和右子树分别是一棵二叉树。 下面这棵树就是一棵二叉树。         二叉树的使用范围最广,一棵多叉树也可以转化为二叉树,因此我们将着重讲解二叉树。二叉树中还有连两种特殊的二叉树叫做满二叉树和
上一节中我们学习了队列,它是一种先进先出的数据结构。还有一种是后进先出的数据结构它叫做栈。栈限定只能在一端进行插入和删除操作。比如说有一个小桶,小桶的直径只能放一个小球,我们现在向小桶内依次放入2号、1号、3号小球。假如你现在需要拿出2号小球,那就必须先将3号小球拿出,再拿出1号小球,最后才能将2号小球拿出来。在刚才取小球的过程中,我们最先放进去的小球最后才能拿出来,而最后放进去的小球却可以最先拿
msdn中的描述如下:(?= 子表达式)(零宽度正预测先行断言。) 仅当子表达式在此位置的右侧匹配时才继续匹配。例如,w+(?=d) 与后跟数字的单词匹配,而不与该数字匹配。此构造不会回溯。(?(零宽度正回顾后发断言。) 仅当子表达式在此位置的左侧匹配时才继续匹配。例如,(?此构造不会回溯。msdn描述的比较清楚,如:w+(?=ing) 可以匹配以ing结尾的单词(匹配结果不包括ing),(
1.引入线索二叉树 二叉树的遍历实质上是对一个非线性结构实现线性化的过程,使每一个节点(除第一个和最后一个外)在这些线性序列中有且仅有一个直接前驱和直接后继。但在二叉链表存储结构中,只能找到一个节点的左、右孩子信息,而不能直接得到节点在任一遍历序列中的前驱和后继信息。这些信息只有在遍历的动态过程中才能得到,因此,引入线索二叉树来保存这些从动态过程中得到的信息。 2.建立线索二叉树 为了保
排序与我们日常生活中息息相关,比如,我们要从电话簿中找到某个联系人首先会按照姓氏排序、买火车票会按照出发时间或者时长排序、买东西会按照销量或者好评度排序、查找文件会按照修改时间排序等等。在计算机程序设计中,排序和查找也是最基本的算法,很多其他的算法都是以排序算法为基础,在一般的数据处理或分析中,通常第一步就是进行排序,比如说二分查找,首先要对数据进行排序。在Donald Knuth 的计算机程序设