快速排序C++
2015-07-04 21:37
447 查看
快速排序算法和合并排序算法一样,也是基于分治模式。对子数组A[p...r]快速排序的分治过程的三个步骤为:
分解:把数组A[p...r]分为A[p...q-1]与A[q+1...r]两部分,其中A[p...q-1]中的每个元素都小于等于A[q]而A[q+1...r]中的每个元素都大于等于A[q];
解决:通过递归调用快速排序,对子数组A[p...q-1]和A[q+1...r]进行排序;
合并:因为两个子数组是就地排序的,所以不需要额外的操作。
快速排序算法的伪代码:
QUICKSORT(A, p
< r) {
1 if p
< r {
2 q = PARTITION(A, p, r);
3 QUICKSORT(a, p, q-1);
4 QUICKSORT(a, q+1, r);
5 }
}
这个算法的关键在于数组的划分,即PARTITION:
PARTITION(A, p, r) {
1 x = A[r];
2 i = p-1;
3 for j = p to r-1 {
4 if A[j] ≤
x {
5 i = i + 1;
6 exchange(A[i], A[j]);
7 }
8 }
9 exchange(A[i+1], A[r]);
10 return i+1;
}
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//交换数据
void exchange(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
//指定范围随机数
int Random(int n, int m)
{
int pos, dis;
pos = n;
dis = m - n + 1;
srand((int)time(NULL));
return rand() % dis + pos;
}
//对数组进行划分
int Partition(int *A, int p, int r) //p,r均为数组下标
{
int x = A[r];
int i = p - 1;
for (int j = p; j < r; j++)
{
if (A[j] < x)
{
i = i + 1;
exchange(A[i], A[j]);
}
}
exchange(A[i + 1], A[r]);
return i + 1;
}
//快速排序
void QuickSort(int *A, int p, int r)
{
if (p < r)
{
int q = Partition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A, q + 1, r);
}
}
/*下面是随机生成主元*/
//随机划分
int RandomPartition(int *A, int p, int r)
{
int i = Random(p, r);
exchange(A[r], A[i]);
return Partition(A, p, r);
}
//随机快速排序
void RandomQuickSort(int *A, int p, int r)
{
if (p < r)
{
int q = RandomPartition(A, p, r);
RandomQuickSort(A, p, q - 1);
RandomQuickSort(A, q + 1, r);
}
}
int main()
{
int n = 1, m = 5;
cout <<"随机数:"<< Random(n, m) << endl;
int a[] = { 1, 3, 4, 6, 3, 6, 9, 12, 10, 8 };
RandomQuickSort(a, 0, 9);
for (int i = 0; i < 10; i++)
cout << a[i] << endl;
}
分解:把数组A[p...r]分为A[p...q-1]与A[q+1...r]两部分,其中A[p...q-1]中的每个元素都小于等于A[q]而A[q+1...r]中的每个元素都大于等于A[q];
解决:通过递归调用快速排序,对子数组A[p...q-1]和A[q+1...r]进行排序;
合并:因为两个子数组是就地排序的,所以不需要额外的操作。
快速排序算法的伪代码:
QUICKSORT(A, p
< r) {
1 if p
< r {
2 q = PARTITION(A, p, r);
3 QUICKSORT(a, p, q-1);
4 QUICKSORT(a, q+1, r);
5 }
}
这个算法的关键在于数组的划分,即PARTITION:
PARTITION(A, p, r) {
1 x = A[r];
2 i = p-1;
3 for j = p to r-1 {
4 if A[j] ≤
x {
5 i = i + 1;
6 exchange(A[i], A[j]);
7 }
8 }
9 exchange(A[i+1], A[r]);
10 return i+1;
}
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//交换数据
void exchange(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
//指定范围随机数
int Random(int n, int m)
{
int pos, dis;
pos = n;
dis = m - n + 1;
srand((int)time(NULL));
return rand() % dis + pos;
}
//对数组进行划分
int Partition(int *A, int p, int r) //p,r均为数组下标
{
int x = A[r];
int i = p - 1;
for (int j = p; j < r; j++)
{
if (A[j] < x)
{
i = i + 1;
exchange(A[i], A[j]);
}
}
exchange(A[i + 1], A[r]);
return i + 1;
}
//快速排序
void QuickSort(int *A, int p, int r)
{
if (p < r)
{
int q = Partition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A, q + 1, r);
}
}
/*下面是随机生成主元*/
//随机划分
int RandomPartition(int *A, int p, int r)
{
int i = Random(p, r);
exchange(A[r], A[i]);
return Partition(A, p, r);
}
//随机快速排序
void RandomQuickSort(int *A, int p, int r)
{
if (p < r)
{
int q = RandomPartition(A, p, r);
RandomQuickSort(A, p, q - 1);
RandomQuickSort(A, q + 1, r);
}
}
int main()
{
int n = 1, m = 5;
cout <<"随机数:"<< Random(n, m) << endl;
int a[] = { 1, 3, 4, 6, 3, 6, 9, 12, 10, 8 };
RandomQuickSort(a, 0, 9);
for (int i = 0; i < 10; i++)
cout << a[i] << endl;
}
相关文章推荐
- C++带const、static和#define关键字的变量定义的区别
- C语言 非打印字符
- C++ 标准库之iomanip
- 堆排序C++
- 自学笔记-C语言复习2015年7月4日
- C++ 中string.find() 函数的用法总结(转载)
- 【学习笔记】【C语言】typedef
- 如何用Windbg找到被catch住的C++的异常
- C++ STL关联容器 set和map学习笔记
- 关于C++ const 的全面总结
- C语言中的预处理命令
- C++ map的简单应用
- c++ stl algorithm: std::find, std::find_if
- C++ inline函数
- C++ QT中的QSound使用方法
- 国嵌C++专题1-蜕变成蝶
- 关于C++语言从磁盘里面读取浮点数据的问题
- [转载] C++ 程序员快过来围观:非常实用全面的 C++ 资源
- error C2440: “static_cast”: 无法从“void (__thiscall CPppView::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscal
- C++中枚举定义运算符