快速排序是基于分治策略的一种排序算法。
快速排序(1)
基本思想是,对于输入的数组a[p : r]按照分解,递归求解,合并三个步骤进行排序。
- 分解:以a [p : r]中的p为基准将原数组分成三段,分别是a [p : q - 1],a [q],a [q + 1:r]。下标q在划分过程中确定。
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); // 右边
}
}
- 递归求解:通过递归调用快速排序算法,分别对a [p:q - 1],a [q + 1 : r]进行排序。
template<class Type>
int Partition(Type a[], int p, int r){
int i = p, j = r + 1;
Type key = a[p]; // 设置Key
while(true){
while(a[++i] < key && i < r)
while(a[--j] > key )
if(i >= j){
break;
}
swap(a[i], a[j]);
}
a[p] = a[j];
a[j] = key;
return j;
}
- 合并:由于对a [p:q - 1]和a [q+1:r]是有序的就地进行的,因此在a [p:q - 1]和a [q+1:r]都已经排好序后,不需要执行任何计算,a [p:r]已经排好序。
ac代码(1)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
int n;
int a[MAX] = {0};
template<class Type>
int Partition(Type a[], int p, int r){
int i = p + 1, j = r;
Type key = a[p]; // 设置Key
while(i <= j){
while(a[i] <= key && i <= j){
i++;
}
while(a[j] >= key && i <= j){
j--;
}
if(i <= j){
swap(a[i], a[j]);
}
}
a[p] = a[j];
a[j] = key;
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); // 右边
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
quickSort(a, 0, n - 1);
for(int i = 0; i < n; i ++){
cout << a[i] << " ";
}
//system("pause");
}
ac代码(2)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
int n;
int a[MAX] = {0};
template<class Type>
int Partition(Type a[], int p, int r) //返回调整后基准数的位置
{
int i = p, j = r;
int key = a[i];
while(i < j){
while(i < j && a[j] >= key){
j --;
}
if(i < j){
a[i] = a[j];
i ++;
}
while(i < j && a[i] < key){
i ++;
}
if(i < j){
a[j] = a[i];
j --;
}
}
a[i] = key;
return i;
}
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); // 右边
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
quickSort(a, 0, n - 1);
for(int i = 0; i < n; i ++){
cout << a[i] << " ";
}
//system("pause");
}
T(n)的计算
对于快速排序的T(n)的计算,由于Partation()的计算时间均为O(n),因此跟划分是否对称有关。
-
最坏的情况:发生在划分过程中产生的两个区域中分别包含n - 1个元素和1个元素的时候。 如果算法Partation的每步都出现在这种不对称划分,则其计算时间复杂性T(n)满足:
解此递归方程,可得T(n) = O(n2); -
最好的情况:发生在划分过程中所取的基准都恰好为中值,即每次划分都产生两个大小为n/2,此时Partation算法的计算时间T(n)满足:
解此递归方程,可得T(n) = O(nlogn);
可以证明,快速排序算法在平均情况下的时间复杂度也是O(nlogn),这是基于比较的排序算法类中算是快速的,因此得名快速排序。
快速排序(2)
对于快速排序的划分,有更进一步的写法。通过修改函数Partation(),可以设计出来采用随机选择策略的快速排序算法。在快速排序的基准数还没有划分时,可以在a [p :r]中随机选出一个元素作为划分基准,这样可以是划分基准的选择是随机的,从而可以期望划分是较对称的。
- 随机划分的算法可实现如下:
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);
}
- 函数Random(p,r)产生p和r之间的一个随机整数,且产生不同整数的概率相同。随机化的快速排序算法通过调用RandomizedPartation来产生随机划分。
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); // 右边
}
}
快速排序练习题.
ac代码(1)
#include <bits/stdc++.h>
using namespace std;
#define random(a, b) ((rand()) % ((b) - (a) + 1) + (a))
const int MAX = 1e5 + 5;
int n;
int a[MAX] = {0};
template<class Type>
int Partition(Type a[], int p, int r){
int i = p + 1, j = r;
Type key = a[p]; // 设置Key
while(i <= j){
while(a[i] <= key && i <= j){
i++;
}
while(a[j] >= key && i <= j){
j--;
}
if(i <= j){
swap(a[i], a[j]);
}
}
a[p] = a[j];
a[j] = key;
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); // 右边
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
RandomizedquickSort(a, 0, n - 1);
for(int i = 0; i < n; i ++){
cout << a[i] << " ";
}
// system("pause");
}