快速排序
快速排序(Quicksort)是對冒泡排序的一種改進。由C. A. R. Hoare在1962年提出。它的基本思想是:通過一趟排序將要排序的數據分割成獨立的兩部分,其中一部分的所有數據都比另外一部分的所有數據都要小,然後再按此方法對這兩部分數據分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。
1) 遞歸實現的快速排序算法
用分治法處理待排序數據,由於排序過程把數據分爲兩個部分,兩個部分是獨立處理的,所以可以用分治的方法處理兩部分。
C++實現:
/****************************
*Name:快速排序_遞歸.cpp
*Tags:排序
*Note:
*****************************/
#include<iostream>
#defineMAX_SIZE 1000
usingnamespace std;
template<typename Type> void QuickSort(Type *, int, int);
template<typename Type> int Process(Type *, int, int);
intmain()
{
int i, len, array[MAX_SIZE];
cout << "Input the size of thearray: ";
cin >> len;
cout << "Start Input: "<< endl;
for(i = 0; i < len; i++) {
cin>> array[i];
}
QuickSort(array, 0, len-1); //快排
cout << "After Sort :"<< endl;
for(i = 0; i < len; i++) {
cout<< array[i] << " ";
}
cout << endl;
return 0;
}
template<typename Type>
voidQuickSort(Type *array, int start, int end)
{
int k;
if(start < end-1) {
k =Process(array, start, end);
QuickSort(array, start, k-1);
QuickSort(array, k+1, end-1);
}
} //遞歸過程
template<typename Type>
intProcess(Type *array, int i, int j)
{
Type temp;
temp = array[i];
while(i < j) {
while(j > i && array[j] >= temp) {
j--;
}
array[i] = array[j];
while(i < j && array[i] <= temp) {
i++;
}
array[j] = array[i];
}
array[i] = temp;
return i;
} //快排處理過程
在最壞的情況下,快速排序的時間複雜度爲O(n^2),快排的平均時間複雜度爲O(nlogn)。
2) 隨機快速排序
快速排序的最壞情況基於每次劃分對主元的選擇。基本的快速排序選取第一個元素作爲主元。這樣在數組已經有序的情況下,每次劃分將得到最壞的結果。一種比較常見的優化方法是隨機化算法,即隨機選取一個元素作爲主元。這種情況下雖然最壞情況仍然是O(n^2),但最壞情況不再依賴於輸入數據,而是由於隨機函數取值不佳。實際上,隨機化快速排序得到理論最壞情況的可能性僅爲1/(2^n)。所以隨機化快速排序可以對於絕大多數輸入數據達到O(nlogn)的期望時間複雜度。
隨機化快速排序的唯一缺點在於,一旦輸入數據中有很多的相同數據,隨機化的效果將直接減弱。對於極限情況,即對於n個相同的數排序,隨機化快速排序的時間複雜度將毫無疑問的降低到O(n^2)
隨機化快排的關鍵在於用rand函數獲取隨機位置
C++實現:
/****************************
*Name:快速排序_隨機化
*Tags:排序 遞歸 隨機
*Note:
*****************************/
#include<iostream>
#include<Cstdlib>
#defineMAX_SIZE 1000
usingnamespace std;
template<class Type> void QuickSort(Type *, int, int);
template<class Type> int Process(Type *, int, int);
intmain()
{
int len, i, array[MAX_SIZE];
cout << "Input the size of thearray: " ;
cin >> len;
cout << "Input Start"<< endl;
for(i = 0; i < len; i++) {
cin >> array[i];
}
cout << "Input End"<< endl;
QuickSort(array, 0, len-1); //快速排序
cout << "After Sort: "<< endl;
for(i = 0; i < len; i++) {
cout << array[i] << " ";
}
cout << endl;
return 0;
}
template<class Type>
voidQuickSort(Type *array, int start, int end)
{
int k;
if(start < end) {
k = Process(array, start, end);
QuickSort(array, start, k-1);
QuickSort(array, k+1, end); //遞歸處理
}
}
template<class Type>
intProcess(Type *array, int i, int j)
{
int r;
Type temp;
r = rand() % (j-i+1) + i; //獲取隨機位置
temp = array[r];
array[r] = array[i]; //獲取隨機位置的值
array[i] = temp; //與第一個數交換位置
while(i < j) {
while(i < j && array[j] >= temp) {
j--;
}
array[i] = array[j];
while(i < j && array[i] <= temp) {
i++;
}
array[j] = array[i];
} //排序過程
array[i] = temp;
return i;
}
3) 平衡快排
平衡快排(BalancedQuicksort):每次儘可能地選擇一個能夠代表中值的元素作爲關鍵數據,然後遵循普通快排的原則進行比較、替換和遞歸。通常來說,選擇這個數據的方法是取開頭、結尾、中間3個數據,通過比較選出其中的中值。取這3個值的好處是在實際問題(例如信息學競賽……)中,出現近似順序數據或逆序數據的概率較大,此時中間數據必然成爲中值,而也是事實上的近似中值。萬一遇到正好中間大兩邊小(或反之)的數據,取的值都接近最值,那麼由於至少能將兩部分分開,實際效率也會有2倍左右的增加,而且利於將數據略微打亂,破壞退化的結構。
C++實現:
/****************************
*Name:快速排序_平衡.cpp
*Tags:排序 平衡
*Note:
*****************************/
#include <iostream>
#define MAX_SIZE 1000
using namespace std;
template <class Type> voidBalance_QuickSort(Type *, int, int);
template <class Type> intProcess(Type *, int, int);
template <class Type> intgetmiddle(Type *, int, int);
int main()
{
int len, i, array[MAX_SIZE];
cout << "Input the size of the array: ";
cin >> len;
for(i = 0; i < len; i++) {
cin >> array[i];
}
Balance_QuickSort(array, 0, len-1); //排序
cout << "After Sort: " << endl;
for(i = 0; i < len; i++) {
cout << array[i] << "";
}
cout << endl;
return 0;
}
template <class Type>
void Balance_QuickSort(Type *array, intstart, int end)
{
int x;
if(start < end) {
x = Process(array, start, end);
Balance_QuickSort(array, start, x-1);
Balance_QuickSort(array, x+1, end);
} //遞歸處理
}
template <class Type>
int Process(Type *array, int i, int j)
{
Type temp;
int mild;
mild = getmiddle(array, i, j); //獲取中值
temp = array[i];
array[i] = array[mild];
array[mild] = temp;
temp = array[i];
while(i < j) {
while(i < j && array[j] >=temp) {
j--;
}
array[i] = array[j];
while(i < j && array[i] <=temp) {
i++;
}
array[j] = array[i];
} //排序過程
array[i] = temp;
return i;
}
template <class Type>
int getmiddle(Type *array, int i, int j)
{
int mid = (i+j) / 2;
if(array[mid] > array[i] && array[mid] < array[j] ||
array[mid] < array[i] && array[mid]> array[j]) {
return mid;
}
else if(array[i] > array[mid] && array[i] < array[j] ||
array[i] < array[mid] &&array[i] > array[j]) {
return i;
}
else return j;
} //獲取中值