快速排序的递归和非递归实现

35 篇文章 0 订阅

快速排序的递归和非递归实现

标签: 快速排序
  6933人阅读  评论(1)  收藏  举报
  分类:

 快速排序的基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,已达到整个  序列有序.

  快速排序是一种不稳定的排序方法,其平均时间复杂度为:O(NlogN).

  特别注意:快速排序中用到的Partition函数,它的作用是进行一趟快速排序,返回"曲轴“记录所在的位置p."曲轴“记录就是一个参考记录,经过Partition处理之后,p左边的记录关键字均不大于曲轴记录的关键字,p右边的记录关键字均不小于曲轴记录的关键字。 Partition函数在找出数组中最大或最小的k个记录也很有用.

快速排序的递归和非递归实现的代码余下:

 

[cpp]  view plain  copy
  1. // 快速排序.cpp : 定义控制台应用程序的入口点。  
  2. #include "stdafx.h"  
  3. #include <iostream>  
  4. #include <stack>  
  5. using namespace std;  
  6.   
  7. /* 
  8. //Partition实现方法1,严蔚敏版数据结构实现方法 
  9. int Partition(int * a,int low,int high) 
  10. { 
  11.   int pivotkey=a[low]; 
  12.   while(low<high) 
  13.   { 
  14.     while(low<high && a[high]>=pivotkey) 
  15.         --high; 
  16.     a[low]=a[high]; 
  17.     while(low<high && a[low]<=pivotkey) 
  18.         ++low; 
  19.     a[high]=a[low]; 
  20.   } 
  21.   //此时low==high 
  22.   a[low]=pivotkey; 
  23.   return low; 
  24. } 
  25. */  
  26.   
  27. //交换a与b的值  
  28. void swap(int &a,int &b)  
  29. {  
  30.   int temp=a;  
  31.   a=b;  
  32.   b=temp;  
  33. }  
  34.   
  35. //Partition实现方法2,算法导论实现方法  
  36. int Partition(int * a,int low,int high)  
  37. {  
  38.   int pivotkey=a[high];//将最后一个元素当做曲轴  
  39.   int p=low-1;  
  40.   for(int j=low;j<high;j++)  
  41.   {  
  42.     if(a[j]<=pivotkey)  
  43.     {  
  44.         p++;  
  45.         if(p!=j)  
  46.         {  
  47.           swap(a[p],a[j]);//在p前面的都是大于pivotkey的元素  
  48.         }                 //在p位置和其后的都是小于等于pivotkey的元素  
  49.     }  
  50.   }  
  51.   p+=1;  
  52.   swap(a[p],a[high]);  
  53.   return p;  
  54. }  
  55.   
  56.   
  57. void QSort(int *a,int start,int end)  
  58. {  
  59.     if(start<end)  
  60.     {  
  61.       int p=Partition(a,start,end);  
  62.       QSort(a,start,p-1);  
  63.       QSort(a,p+1,end);  
  64.     }  
  65. }  
  66.   
  67. //递归形式的快速排序   
  68. void QuickSort(int *a,int len)  
  69. {  
  70.   QSort(a,0,len-1);  
  71. }  
  72.   
  73. //非递归方式实现快速排序  
  74.   
  75. //定义一个记录待排序的区间[low,high]  
  76. typedef struct Region  
  77. {  
  78.      int low;  
  79.      int high;  
  80. }Region;  
  81.   
  82. void NonRecursiveQuickSort(int *a,int len)  
  83. {  
  84.    stack<Region> regions;//定义一个栈变量  
  85.    Region region;  
  86.    region.low=0;  
  87.    region.high=len-1;  
  88.    regions.push(region);  
  89.    while(!regions.empty())  
  90.    {  
  91.      region=regions.top();  
  92.      regions.pop();  
  93.      int p=Partition(a,region.low,region.high);  
  94.      if(p-1>region.low)  
  95.      {  
  96.          Region regionlow;  
  97.          regionlow.low=region.low;  
  98.          regionlow.high=p-1;  
  99.          regions.push(regionlow);  
  100.      }  
  101.      if(p+1<region.high)  
  102.      {  
  103.          Region regionhigh;  
  104.          regionhigh.low=p+1;  
  105.          regionhigh.high=region.high;  
  106.          regions.push(regionhigh);  
  107.      }  
  108.    }  
  109. }  
  110.   
  111. void printArray(int *a,int len)  
  112. {  
  113.   for(int i=0;i<len;i++)  
  114.       cout<<a[i]<<" ";  
  115.   cout<<endl;  
  116. }  
  117.   
  118. int _tmain(int argc, _TCHAR* argv[])  
  119. {  
  120.     int a[]={49,38,65,97,76,13,27,49};  
  121.     int len=sizeof(a)/sizeof(int);  
  122.     printArray(a,len);  
  123.     //QuickSort(a,len);  
  124.     NonRecursiveQuickSort(a,len);  
  125.     printArray(a,len);  
  126.     system("PAUSE");  
  127.     return 0;  
  128. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值