分治法 --快速排序
来源:互联网 发布:ubuntu变为中文 编辑:程序博客网 时间:2024/06/05 09:23
【分治法】快速排序
1、未优化的快速排序
快速排序的基本思想是通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
程序清单如下:
- //2d8-1 未优化的快速排序
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int a[] = {5,7,3,4,8,6,9,1,2};
- template <class Type>
- void Swap(Type &x,Type &y);
- template <class Type>
- int Partition(Type a[],int p,int r);
- template <class Type>
- void QuickSort(Type a[],int p,int r);
- int main()
- {
- for(int i=0; i<9; i++)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- QuickSort(a,0,8);
- for(int i=0; i<9; i++)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- }
- template <class Type>
- void Swap(Type &x,Type &y)
- {
- Type temp = x;
- x = y;
- y = temp;
- }
- template <class Type>
- int Partition(Type a[],int p,int r)
- {
- int i = p,j = r + 1;
- Type x = a[p];
- while(true)
- {
- while(a[++i]<x && i<r);
- while(a[--j]>x);
- if(i>=j)
- {
- break;
- }
- Swap(a[i],a[j]);
- }
- a[p] = a[j];
- a[j] = x;
- return j;
- }
- template <class Type>
- void QuickSort(Type a[],int p,int r)
- {
- if(p<r)
- {
- int q = Partition(a,p,r);
- QuickSort(a,p,q-1);
- QuickSort(a,q+1,r);
- }
- }
2、随机后标记元素后的快速排序
未随机化处理的快速排序最坏情况发生在划分过程产生的两个区域分别包含n-1个元素和1个元素的时候。由于函数Partition的计算时间为O(n),所以如果算法Partition的每一步都出现这种不对称划分,则T(n)=O(n^2)。
在最好情况下,每次划分所取的基准都恰好为中值,即每次划分都产生两个大小为n/2的区域。此时T(n)=O(nlogn)。
因此,快速排序算法的性能取决于划分的对称性。修改Partition,可以设计出采用随机选择策略的快速排序算法。在快速排序算法的每一步中,当数组还没有被划分时,可以在a[p:r]中随机选择一个元素作为划分基准。
程序如下:
- //2d8-2 随机后标记元素后的快速排序
- #include "stdafx.h"
- #include <iostream>
- #include <ctime>
- using namespace std;
- int a[] = {5,7,3,4,8,6,9,1,2};
- template <class Type>
- void Swap(Type &x,Type &y);
- inline int Random(int x, int y);
- template <class Type>
- int Partition(Type a[],int p,int r);
- template<class Type>
- int RandomizedPartition(Type a[],int p,int r);
- template <class Type>
- void RandomizedQuickSort(Type a[],int p,int r);
- int main()
- {
- for(int i=0; i<9; i++)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- RandomizedQuickSort(a,0,8);
- for(int i=0; i<9; i++)
- {
- cout<<a[i]<<" ";
- }
- cout<<endl;
- }
- template <class Type>
- void Swap(Type &x,Type &y)
- {
- Type temp = x;
- x = y;
- y = temp;
- }
- inline int Random(int x, int y)
- {
- srand((unsigned)time(0));
- int ran_num = rand() % (y - x) + x;
- return ran_num;
- }
- template <class Type>
- int Partition(Type a[],int p,int r)
- {
- int i = p,j = r + 1;
- Type x = a[p];
- while(true)
- {
- while(a[++i]<x && i<r);
- while(a[--j]>x);
- if(i>=j)
- {
- break;
- }
- Swap(a[i],a[j]);
- }
- a[p] = a[j];
- a[j] = x;
- return j;
- }
- template<class Type>
- int RandomizedPartition(Type a[],int p,int r)
- {
- int i = Random(p,r);
- Swap(a[i],a[p]);
- return Partition(a,p,r);
- }
- template <class Type>
- void RandomizedQuickSort(Type a[],int p,int r)
- {
- if(p<r)
- {
- int q = RandomizedPartition(a,p,r);
- RandomizedQuickSort(a,p,q-1);
- RandomizedQuickSort(a,q+1,r);
- }
- }
0 0
- 分治法--快速排序
- 分治法--快速排序
- 分治法-快速排序
- 分治法-快速排序
- 分治法-----快速排序
- 分治法 --快速排序
- 分治法-快速排序
- 分治法--快速排序
- 快速排序 分治法
- 分治法-快速排序
- 分治法之快速排序
- 分治法之快速排序
- 排序4--快速(分治法)
- 分治法之快速排序
- 分治法(Scheme) -- 快速排序
- 分治法之快速排序
- 分治法-----快速排序算法
- 快速排序(分治法)
- jmeter插件使用(二)
- MySQL for Java的SQL注入测试
- 哈喽卖给的你是怎么回事机制的介么
- Splits the string
- CocoaPods详解之-Podfile.lock
- 分治法 --快速排序
- Android仿人人客户端(v5.7.1)开发(不错)
- Eclipse错误提示: Symbol 'xxxx' could not be resolved
- iOS开发经验之转移iOS App
- 忘记密码 登录帮助 公网登录通道 ·网易邮件中心诚聘英才 | 在线答疑 ·下载全球最好的免费中英翻译词典 ·货比三家选邮箱,做个精明消费者 海量...
- 黑马程序员————泛型初步
- 如果你做的事情毫不费力,就是在浪费时间
- 几个常用运维小工具
- 安装程序工具 InstallUtil.exe