- 浏览: 21300 次
- 性别:
- 来自: 南京
最新评论
快速排序是对冒泡排序的一种改进。它的基本思想是:通过一次排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一不部分的所有数据都要小,然后再按次方法对这两部分数据分别进行快速排序,整个排序过程可以递归或者非递归进行,以此达到整个数据变成有序序列。
下面是快速排序的递归实现:
view sourceprint?01 #include "stdafx.h"
02
03 #define LIST_INIT_SIZE 100 //顺序表初始大小
04 #define LISTINCREMENT 10 //顺序表增量
05 typedef int ElemType; //顺序表元素类型
06 typedef struct //顺序表结构
07 {
08 ElemType *elem;
09 int length;
10 int listsize;
11 }SqList;
12 //初始化顺序表
13 int InitList_Sq(SqList &L)
14 {
15 L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
16 if(!L.elem) return -1;
17 L.length = 0;
18 L.listsize = LIST_INIT_SIZE;
19 return 0;
20 }
21 //创建顺序表
22 int CreatList_Sq(SqList &L)
23 {
24 InitList_Sq(L);
25 int i = 1;
26 while(scanf("%d",&L.elem[i]) != EOF)
27 {
28 i++;
29 }
30 L.length = i - 1;
31 return 0;
32 }
33 //一趟快速排序
34 int Partition(SqList &L,int low,int high)
35 {
36 L.elem[0] = L.elem[low];
37 int pivotkey;
38 pivotkey = L.elem[low];
39 int temp;
40 while(low < high)
41 {
42 while(low < high && L.elem[high] >= pivotkey) --high;;
43 L.elem[low] = L.elem[high];
44 while(low < high && L.elem[low] <= pivotkey) ++low;
45 L.elem[high] = L.elem[low];
46 }
47 L.elem[low] = L.elem[0];
48
49 return low;
50 }
51 //递归实现快速排序
52 void QuickSort(SqList &L,int low,int high)
53 {
54 if(low < high)
55 {
56 int pivotloc = Partition(L,low,high);
57 QuickSort(L,low,pivotloc - 1);
58 QuickSort(L,pivotloc + 1,high);
59 }
60 }
61 int _tmain(int argc, _TCHAR *argv[])
62 {
63 SqList L;
64 CreatList_Sq(L);
65 QuickSort(L,1,L.length);
66 for(int i = 1; i <= L.length; i++)
67 {
68 printf("%d ",L.elem[i]);
69 if(LIST_INIT_SIZE == i) printf("\n");
70 }
71 char ch = getchar();
72
73 return 0;
74 }
快速排序的非递归算法:
view sourceprint?01 #include <iostream>
02 #include <stack>
03 using namespace std;
04 template <class T>
05 int partition(T a[],int low,int high)
06 {
07 T v=a[low];
08 while(low<high)
09 {
10 while(low<high && a[high]>=v) high--;
11 a[low]=a[high];
12 while(low<high && a[low]<=v) low++;
13 a[high]=a[low];
14 }
15 a[low]=v;
16 return low;
17
18 }
19
20 template <class T>
21 void QuickSort(T a[],int p,int q)
22 {
23 stack<int> st;
24 int j;
25 do{
26 while(p<q)
27 {
28 j=partition(a,p,q);
29 if( (j-p)<(q-j) )
30 {
31 st.push(j+1);
32 st.push(q);
33 q=j-1;
34 }
35 else
36 {
37 st.push(p);
38 st.push(j-1);
39 p=j+1;
40 }
41 }
42 if(st.empty()) return;
43 q=st.top();st.pop();
44 p=st.top();st.pop();
45 //cout<<endl<<"p:"<<p<<" ";
46 //cout<<"q:"<<q<<endl;
47 }while(1);
48 }
49
50 void main()
51 {
52 //int a[7]={7,6,5,4,3,2,1};
53 //int a[7]={1,2,3,4,5,6,7};
54 int a[7]={3,5,2,3,66,225,1};
55 for(int i=0;i<7;i++)
56 cout<<a[i]<<" ";
57 QuickSort(a,0,6);
58 cout<<endl;
59 for(int i=0;i<7;i++)
60 cout<<a[i]<<" ";
61 }
下面是快速排序的递归实现:
view sourceprint?01 #include "stdafx.h"
02
03 #define LIST_INIT_SIZE 100 //顺序表初始大小
04 #define LISTINCREMENT 10 //顺序表增量
05 typedef int ElemType; //顺序表元素类型
06 typedef struct //顺序表结构
07 {
08 ElemType *elem;
09 int length;
10 int listsize;
11 }SqList;
12 //初始化顺序表
13 int InitList_Sq(SqList &L)
14 {
15 L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
16 if(!L.elem) return -1;
17 L.length = 0;
18 L.listsize = LIST_INIT_SIZE;
19 return 0;
20 }
21 //创建顺序表
22 int CreatList_Sq(SqList &L)
23 {
24 InitList_Sq(L);
25 int i = 1;
26 while(scanf("%d",&L.elem[i]) != EOF)
27 {
28 i++;
29 }
30 L.length = i - 1;
31 return 0;
32 }
33 //一趟快速排序
34 int Partition(SqList &L,int low,int high)
35 {
36 L.elem[0] = L.elem[low];
37 int pivotkey;
38 pivotkey = L.elem[low];
39 int temp;
40 while(low < high)
41 {
42 while(low < high && L.elem[high] >= pivotkey) --high;;
43 L.elem[low] = L.elem[high];
44 while(low < high && L.elem[low] <= pivotkey) ++low;
45 L.elem[high] = L.elem[low];
46 }
47 L.elem[low] = L.elem[0];
48
49 return low;
50 }
51 //递归实现快速排序
52 void QuickSort(SqList &L,int low,int high)
53 {
54 if(low < high)
55 {
56 int pivotloc = Partition(L,low,high);
57 QuickSort(L,low,pivotloc - 1);
58 QuickSort(L,pivotloc + 1,high);
59 }
60 }
61 int _tmain(int argc, _TCHAR *argv[])
62 {
63 SqList L;
64 CreatList_Sq(L);
65 QuickSort(L,1,L.length);
66 for(int i = 1; i <= L.length; i++)
67 {
68 printf("%d ",L.elem[i]);
69 if(LIST_INIT_SIZE == i) printf("\n");
70 }
71 char ch = getchar();
72
73 return 0;
74 }
快速排序的非递归算法:
view sourceprint?01 #include <iostream>
02 #include <stack>
03 using namespace std;
04 template <class T>
05 int partition(T a[],int low,int high)
06 {
07 T v=a[low];
08 while(low<high)
09 {
10 while(low<high && a[high]>=v) high--;
11 a[low]=a[high];
12 while(low<high && a[low]<=v) low++;
13 a[high]=a[low];
14 }
15 a[low]=v;
16 return low;
17
18 }
19
20 template <class T>
21 void QuickSort(T a[],int p,int q)
22 {
23 stack<int> st;
24 int j;
25 do{
26 while(p<q)
27 {
28 j=partition(a,p,q);
29 if( (j-p)<(q-j) )
30 {
31 st.push(j+1);
32 st.push(q);
33 q=j-1;
34 }
35 else
36 {
37 st.push(p);
38 st.push(j-1);
39 p=j+1;
40 }
41 }
42 if(st.empty()) return;
43 q=st.top();st.pop();
44 p=st.top();st.pop();
45 //cout<<endl<<"p:"<<p<<" ";
46 //cout<<"q:"<<q<<endl;
47 }while(1);
48 }
49
50 void main()
51 {
52 //int a[7]={7,6,5,4,3,2,1};
53 //int a[7]={1,2,3,4,5,6,7};
54 int a[7]={3,5,2,3,66,225,1};
55 for(int i=0;i<7;i++)
56 cout<<a[i]<<" ";
57 QuickSort(a,0,6);
58 cout<<endl;
59 for(int i=0;i<7;i++)
60 cout<<a[i]<<" ";
61 }
发表评论
-
KMP快速字符串查找算法
2011-08-25 19:29 642在C/C++语言编程过程中,一般的字符串搜索操作都是通过标准库 ... -
求解最大公约数问题
2011-08-25 19:27 665最大公因数,又称最大公约数。是指 [n(≧2)个自然数 a1, ... -
堆排序(Heap Sort)算法学习
2011-08-25 19:26 1061在程序设计相关领域, ... -
整数拆分问题的动态规划解法
2011-08-25 19:26 3036输入n,和k,问将n用1到k这k个数字进行拆分,有多少种拆分方 ... -
背包问题介绍与分析
2011-08-25 19:24 1003背包问题是在1978年由Merkel和Hellman提出的。它 ... -
求平方根sqrt()函数的底层算法效率问题
2011-08-25 19:23 1267我们平时经常会有一些数据运算的操作,需要调用sqrt,exp, ... -
面试中常见的一些算法问题
2011-08-25 19:22 675Problem 1 : Is it a loop ? ( ... -
各种排序算法的C++实现与性能比较
2011-08-25 19:21 892排序是计算机算法中非常重要的一项,而排序算法又有不少实现方法, ... -
背包问题之硬币找零问题
2011-08-25 19:19 1132设有6 种不同面值的硬 ... -
求能被1到20的数整除的最小正整数
2011-08-25 19:18 1342求能被1到20的数整除的最小正整数。最直觉的方法是求1到20这 ... -
买书折扣最优惠问题解法
2011-08-25 19:17 723题目:在节假日的时候 ... -
二叉树中的最近公共祖先问题
2011-08-25 19:16 1296题目:要求寻找二叉树中两个节点的最近的公共祖先,并将其返回。 ... -
判断一个整数是否是2的N次方
2011-08-25 19:04 1794题目:给定一个整数num,判断这个整数是否是2的N次方。比如, ... -
字符串逆序的算法汇总
2011-08-25 19:01 1036很早就准备写一个字符串系列的面试题,本来已经写好了,大概有十几 ... -
计算从1到N中1的出现次数
2011-08-25 18:59 576给定一个十进制整数N, ... -
KMP快速字符串查找算法
2011-08-25 18:57 942在C/C++语言编程过程中,一般的字符串搜索操作都是通过标准库 ... -
数字1亿里面有多少个1呢
2011-08-25 18:52 712乍看这题真够唬人的,群里看到这个题目后争先恐后的说看法。最简单 ... -
最大子序列、最长公共子串、最长公共子序列
2011-08-25 18:33 756最大子序列 最大子序列是要找出由数组成的一维数组中和最大的连续 ... -
一道关于男女比例的面试题
2011-08-25 16:56 1019阿里巴巴的一道面试题:说澳大利亚的父母喜欢女孩,如果生出来的第 ...
相关推荐
快速排序 非递归实现方式的完整源代码和测试结果。
可以用递归快速实现排序算法,简洁高效,算法复杂度比较合理
利用栈来消除递归 模拟快速排序的过程 实现非递归的快速排序
两种方法: 传统的递归快速排序 采用非递归堆栈模拟
java 快速排序 折半查找的界面实现 (递归与分治法)
使用递归的方法实现快速排序,简洁明了,避免了烦琐的考虑循环。
此文档是快速排序的递归与非递归的具体实现代码
快速排序已经是很成熟的排序方法 递归的缺点就是当排序数据量大时,系统堆栈会溢出 递归的实质是在堆栈中不断保存现场,但是现场的数据量是很大的 网上给出了堆栈实现的伪码算法,但是这里面存在很多的BUG 这个程序...
用栈实现的快速排序,避免了原来的递归算法
快速排序一般用的是递归算法,利用系统的提供的栈结构,而此非递归算法没有利用栈,巧妙完成了排序,并提供人机交互界面
用C实现了快速排序的非递归算法. int quickpass ( sqlist &R, int low, int high ) { ... } void quicksort ( sqlist &r, int low, int high ) { ... }
快速排序是实际运用中用的最多的算法,虽然它在最坏的情况下会达到n^2,但它的平均性能非常好,期望时间复杂度为nlgn,而且隐含的常数因子非常小,并且是原址排序。 快速排序原理:从一组数中任意选出一个数,将...
用分治算法的思想,加上递归 实现快速排序
详细解释了快速排序的java实现.里面有代码,还有注释说明
快速排序快速排序(Quicksort)是对冒泡排序的一种改进。适用很广,高效的排序方法。
MATLAB实现快速排序,这是一个递归算法。输入的数据为待排序序列,leftIndex和rightIndex是开始的左右两个边界。
在双向链表上实现快速排序的递归算法 输入的形式:元素个数、元素都为整型。 输入值范围:元素个数为非负正整数,需要排序的元素都为整型。 输出的形式:排序前的元素序列和排序后的元素序列。 程序的功能:对用户...
快速排序算法设计与分析总结 二叉树与树的转换前序、后序的递归、非递归算法,层次序的非递归算法的实现 二叉树与树的转换前序、后序的递归、非递归算法,层次序的非递归算法的实现,应包含建树的实现 实现树与...
直接插入排序 冒泡排序 快速排序 直接选择排序 堆排序 二路归并排序 C#源代码 使用C#实现的数据结构中的排序算法