• 堆排序、快速排序、归并排序总结


     

    2012-09-21 11:33:41|  分类: 算法、数据结构 |  标签:算法数据结构  归并排序  内部排序  面试  |举报 |字号订阅

     
     

    这三个排序以前都写过,快速排序还写了递归版和迭代版。现在在这里做一下总结。

    堆排序(Heap Sort)

    堆排序是一种树形选择排序,在排序过程中,将A[n]看成是完全二叉树的顺序存储结构,利用完全二叉树中双亲结点和孩子结点之间的内在关系来选择最小的元素。对N个元素从小到大排序建立大根堆,然后交换堆顶与最后一个元素,将剩下的N-1个元素调整为大根堆,执行N-1此这样的操作。 堆排序是不稳定的。算法时间复杂度O(nlogn)。

    1. //调整大根堆 
    2. void HeapAdjust(int data[],int nStart, int nLen) 
    3.     int nMaxChild = 0; 
    4.     int Temp; 
    5.  
    6.     while ((2*nStart+1) < nLen) 
    7.     { 
    8.         nMaxChild = 2*nStart+1; 
    9.         if ( (2*nStart+2) < nLen) 
    10.         { 
    11.             //比较左子树和右子树,记录最大值的Index 
    12.             if (data[2*nStart+1] < data[2*nStart+2]) 
    13.             { 
    14.                 nMaxChild = 2*nStart+2; 
    15.             } 
    16.         } 
    17.         //change data 
    18.         if (data[nStart] < data[nMaxChild]) 
    19.         { 
    20.             //交换nStart与nMaxChild的数据 
    21.             Temp = data[nStart]; 
    22.             data[nStart] = data[nMaxChild]; 
    23.             data[nMaxChild] = Temp; 
    24.  
    25.             //堆被破坏,需要重新调整 
    26.             nStart = nMaxChild; 
    27.         } 
    28.         else 
    29.         { 
    30.             //比较左右孩子均小则堆未破坏,不再需要调整 
    31.             break
    32.         } 
    33.     } 
    34.  
    35. //堆排序 从小到大排序建立大顶堆 
    36. void HeapSort(int data[],int nLen) 
    37.     int i; 
    38.     int nTemp; 
    39.     //建立堆 
    40.     for (i = nLen/2-1; i >= 0; i--) 
    41.     { 
    42.         HeapAdjust(data, i, nLen); 
    43.     } 
    44.     for (i = nLen-1; i > 0; i--) 
    45.     { 
    46.         //交换堆顶元素和最后一个元素 
    47.         nTemp = data[0]; 
    48.         data[0] = data[i]; 
    49.         data[i] = nTemp; 
    50.  
    51.         //将data[0...i]重写建成大根堆 
    52.         HeapAdjust(data, 0, i); 
    53.     } 

     

    快速排序(Quick Sort)

    快速排序是对冒泡排序的一种本质改进。它的基本思想是通过一趟扫描后,使得排序序列的长度能大幅度地减少。在冒泡排序中,一次扫描只能确保最大数值的数移到正确位置,而待排序序列的长度可能只减少1。快速排序通过一趟扫描,就能确保某个数(以它为基准点吧)的左边各数都比它小,右边各数都比它大。然后又用同样的方法处理它左右两边的数,直到基准点的左右只有一个元素为止。

    快速排序是不稳定的。最理想情况算法时间复杂度O(nlog2n),最坏O(n^2)。

    1. //快速排序 
    2. void QuickSort(int a[], int low, int high) 
    3.     if (low < high) 
    4.     { 
    5.         // 划分 
    6.         int pivot = a[low]; 
    7.         int i = low; int j = high; 
    8.         while (i < j) 
    9.         { 
    10.             while (i<j && a[j] >= pivot)  j--; 
    11.             a[i] = a[j]; 
    12.             while (i<j && a[i] <= pivot)  i++; 
    13.             a[j] = a[i]; 
    14.         } 
    15.         a[i] = pivot; 
    16.         // 对子序列快排 
    17.         QuickSort(a, low, i-1); 
    18.         QuickSort(a, i+1, high); 
    19.     } 
    20.  
    21. //快速排序法的划分操作 
    22. int Partition(int vec[],int low,int high) 
    23.      //任选元素作为轴,这里选首元素 
    24.      int pivot = vec[low]; 
    25.      while(low < high) 
    26.      { 
    27.          while(low < high && vec[high] >= pivot) 
    28.              high--; 
    29.          vec[low] = vec[high]; 
    30.          while(low < high && vec[low] <= pivot) 
    31.              low++; 
    32.          vec[high] = vec[low]; 
    33.      } 
    34.      //此时low==high 
    35.      vec[low] = pivot; 
    36.      return low; 
    37.  
    38. //in-place partition algorithm 
    39. //http://en.wikipedia.org/wiki/Quicksort 
    40. int in_place_partition(int* array, int left, int right) 
    41.     int index = left; 
    42.     int pivot = array[index]; 
    43.     swap(array[index], array[right]); 
    44.     for (int i=left; i<right; i++) 
    45.     { 
    46.         if (array[i] < pivot)    //升序 
    47.         { 
    48.             swap(array[index], array[i]); 
    49.             ++index; 
    50.         } 
    51.     } 
    52.     swap(array[right], array[index]); 
    53.     return index; 
    54.  
    55. void Qsort(int* array, int left, int right) 
    56.     if (left >= right) 
    57.         return
    58.     int index = Partition(array, left, right); 
    59.     Qsort(array, left, index - 1); 
    60.     Qsort(array, index + 1, right); 
    61.  
    62. //非递归快速排序 
    63. void QuickSort2(int vec[],int low,int high) 
    64.     stack<int> st; 
    65.     if(low < high) 
    66.     { 
    67.         int mid = Partition(vec,low,high); 
    68.         if(low < mid-1) 
    69.         { 
    70.              st.push(low); 
    71.              st.push(mid-1); 
    72.          } 
    73.          if(mid+1 < high) 
    74.          { 
    75.              st.push(mid+1); 
    76.              st.push(high); 
    77.          } 
    78.          //用栈保存每个待排序子串的首尾元素下标,下一次循环时取出这个范围,对这段子序列进行partition操作 
    79.          while(!st.empty()) 
    80.          { 
    81.              int q = st.top(); 
    82.              st.pop(); 
    83.              int p=st.top(); 
    84.              st.pop(); 
    85.              mid = Partition(vec,p,q); 
    86.              if(p < mid-1) 
    87.              { 
    88.                  st.push(p); 
    89.                  st.push(mid-1); 
    90.              } 
    91.              if(mid+1<q) 
    92.              { 
    93.                  st.push(mid+1); 
    94.                  st.push(q); 
    95.              } 
    96.          } 
    97.      } 

     

    归并排序(Merge Sort)

    基本思想是合并两个有序表,设有两个有序(升序)序列存储在同一数组中相邻的位置上,不妨设为A[l..m],A[m+1..h],将它们归并为一个有序数列,并存储在A[l..h]。 归并排序的时间复杂度无论是在最好情况下还是在最坏情况下均是O(nlog2n)。

    归并排序在最坏的情况下都是O(NlogN)的时间复杂度,缺点是Merge的时候要有O(N)的额外的空间,如何改进?使用In-place Merge Sort:http://blog.ibread.net/345/in-place-merge-sort/       关于原地归并也可参考我前面的文章

    1. //将有序序列a[low..mid]和a[mid+1..high]归并到a[low..high]。 
    2. void Merge(int a[], int low, int mid, int high) 
    3.     // 归并到b[] 
    4.     int i = low; 
    5.     int j = mid+1; 
    6.     int k = 0; 
    7.     int *b = newint[high-low+1]; 
    8.     while (i <= mid && j <= high) 
    9.     { 
    10.         if (a[i] <= a[j]) { b[k++] = a[i++]; } 
    11.         else  { b[k++] = a[j++]; } 
    12.     } 
    13.     // 归并剩余元素 
    14.     while (i <= mid)  b[k++] = a[i++]; 
    15.     while (j <= high)  b[k++] = a[j++]; 
    16.     // 从b[]复制回a[] 
    17.     for(i = 0; i <= high-low; ++i) 
    18.         a[low+i] = b[i]; 
    19.     delete []b; 
    20.  
    21. //归并排序 
    22. void MergeSort(int a[], int low, int high) 
    23.     if(low >= high)  return
    24.     else 
    25.     { 
    26.         int mid = (low+high)/2; 
    27.         MergeSort(a,low,mid); 
    28.         MergeSort(a,mid+1,high); 
    29.         Merge(a,low,mid,high); 
    30.     } 
    31.  
    32. //自底向上的归并排序 
    33. void MergeSort2(int a[], int n) 
    34.     int s,i,t = 1; 
    35.     while(t < n) 
    36.     { 
    37.         s = t;  t = s*2; 
    38.         for(i=0; i+t<=n; i+=t) 
    39.             Merge(a,i,i+s-1,i+t-1); 
    40.         if(i+s < n) 
    41.             Merge(a,i,i+s-1,n-1); 
    42.     } 
    43. }
  • 相关阅读:
    4.10Python数据处理篇之Matplotlib系列(十)---文本的显示
    4.9Python数据处理篇之Matplotlib系列(九)---子图分布
    4.8Python数据处理篇之Matplotlib系列(八)---Figure的学习
    4.7Python数据处理篇之Matplotlib系列(七)---matplotlib原理分析
    4.6Python数据处理篇之Matplotlib系列(六)---plt.hist()与plt.hist2d()直方图
    4.5Python数据处理篇之Matplotlib系列(五)---plt.pie()饼状图
    4.4Python数据处理篇之Matplotlib系列(四)---plt.bar()与plt.barh条形图
    4.3Python数据处理篇之Matplotlib系列(三)---plt.plot()折线图
    Angular 定时器$timeout和$interval,延时调用
    javascript中top、clientTop、scrollTop、offsetTop的讲解(转载加总结)
  • 原文地址:https://www.cnblogs.com/siguoya/p/3564060.html
Copyright © 2020-2023  润新知