快速排序的基本實現ios
快速排序算法是一種基於交換的高效的排序算法,它採用了分治法的思想:算法
一、從數列中取出一個數做爲基準數(樞軸,pivot)。 數組
二、將數組進行劃分(partition),將比基準數大的元素都移至樞軸右邊,將小於等於基準數的元素都移至樞軸左邊。函數
三、再對左右的子區間重複第二步的劃分操做,直至每一個子區間只有一個元素。測試
快排最重要的一步就是劃分了。劃分的過程用通俗的語言講就是「挖坑」和「填坑」。ui
快速排序時間複雜度spa
快速排序的時間複雜度在最壞狀況下是O(N2),平均的時間複雜度是O(N*lgN)。code
這句話很好理解:假設被排序的數列中有N個數。遍歷一次的時間複雜度是O(N),須要遍歷多少次呢?至少lg(N+1)次,最多N次。
(01) 爲何最少是lg(N+1)次?快速排序是採用的分治法進行遍歷的,咱們將它看做一棵二叉樹,它須要遍歷的次數就是二叉樹的深度,而根據徹底二叉樹的定義,它的深度至少是lg(N+1)。blog
所以,快速排序的遍歷次數最少是lg(N+1)次。
(02) 爲何最可能是N次?這個應該很是簡單,仍是將快速排序看做一棵二叉樹,它的深度最大是N。所以,快讀排序的遍歷次數最可能是N次。排序
快速排序穩定性
快速排序是不穩定的算法,它不知足穩定算法的定義。
算法穩定性 -- 假設在數列中存在a[i]=a[j],若在排序以前,a[i]在a[j]前面;而且排序以後,a[i]仍然在a[j]前面。則這個排序算法是穩定的!
快速排序 實現一:
1 int partition(int arr[], int left, int right) //找基準數 劃分 2 { 3 int i = left + 1 ; 4 int j = right; 5 int temp = arr[left]; 6 7 while(i <= j) 8 { 9 while (arr[i] < temp) 10 { 11 i++; 12 } 13 while (arr[j] > temp ) 14 { 15 j--; 16 } 17 if (i < j) 18 swap(arr[i++], arr[j--]); 19 else i++; 20 } 21 swap(arr[j], arr[left]); 22 return j; 23 24 } 25 26 void quick_sort(int arr[], int left, int right) 27 { 28 if (left > right) 29 return; 30 int j = partition(arr, left, right); 31 quick_sort(arr, left, j - 1); 32 quick_sort(arr, j + 1, right); 33 }
快速排序 實現方法二:
1 void QuickSort(int array[], int start, int last) 2 { 3 int i = start; 4 int j = last; 5 int temp = array[i]; 6 if (i < j) 7 { 8 while (i < j) 9 { 10 // 11 while (i < j && array[j]>=temp ) 12 j--; 13 if (i < j) 14 { 15 array[i] = array[j]; 16 i++; 17 } 18 19 while (i < j && temp > array[i]) 20 i++; 21 if (i < j) 22 { 23 array[j] = array[i]; 24 j--; 25 } 26 27 } 28 //把基準數放到i位置 29 array[i] = temp; 30 //遞歸方法 31 QuickSort(array, start, i - 1); 32 QuickSort(array, i + 1, last); 33 } 34 }
快速排序 用C++函數模板實現
1 template<typename T> 2 void quicksort(T data[], int first, int last) 3 { 4 int lower = first + 1; 5 int upper = last; 6 swap(data[first], data[(first + last) / 2]); 7 T bound = data[first]; 8 while (lower <= upper) 9 { 10 while (data[lower] < bound) 11 lower++; 12 while (data[upper] > bound) 13 upper--; 14 if (lower < upper) 15 swap(data[lower++], data[upper--]); 16 else lower++; 17 } 18 swap(data[upper], data[first]); 19 if (first < upper - 1) 20 quicksort(data, first, upper - 1); 21 if (upper + 1 < last) 22 quicksort(data, upper + 1, last); 23 } 24 25 template<class T> 26 void quicksort(T data[], int n) 27 { 28 int i, max; 29 if (n < 2) 30 return; 31 for (i = 1, max = 0; i < n; i++) 32 if (data[max] < data[i]) 33 max = i; 34 swap(data[n - 1], data[max]); 35 quicksort(data, 0, n - 2); // 36 }
快速排序 主函數測試代碼
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <vector> #include <algorithm> #include <time.h> using namespace std; void PrintArray(int array[], int len) { for (int i = 0; i < len; i++) { cout << array[i] << " "; } cout << endl; } int main(void) { const int NUM = 10; int array[NUM] = { 0 }; srand((unsigned int)time(nullptr)); for (int i = 0; i < NUM; i++) { array[i] = rand() % 100 + 1; } cout << "排序前:" << endl; PrintArray(array, NUM); cout << "排序後:" << endl; quicksort(array, 0, NUM - 1); PrintArray(array, NUM); return 0; }