Quick sorting is an efficient sorting method. The final performance of the algorithm depends on the intermediate value. The quick sorting method is directly implemented as follows:
#include
#include static int partition( int* array, int start, int end){ int key = array[start]; int l = start; int r = end; while(l < r){ while(l < r && key < array[r]){ -- r; } std:: swap
(array[l], array[r]); while(l < r && key > array[l]){ ++ l; } std:: swap
(array[l], array[r]); } return l;}static void qsort (int * array, int start, int end){ if(start < end){ int p = partition(array, start, end); qsort(array, start, p-1); qsort(array, p+1, end); }}void quick_sort( int* array, int length){ qsort(array, 0, length-1);}int main(){ int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; quick_sort(array, 10); for(int i = 0; i != 10; ++i){ std::cout << array[i] << " "; } std::cout << std:: endl; return 0;}
To solve the problem of selecting values, we can perform the following optimization, and take the median of the first element, the middle element, and the last element as the selected value, the maximum or minimum values are less likely to be selected. The specific code is as follows:
#include
#include static int partition( int* array, int start, int end){ //int key = array[start]; int key; int m = (start+end)/2; if(array[start] > array[end]){ std:: swap
(array[start], array[end]); } if(array[m] > array[end]){ std:: swap
(array[m], array[end]); } if(array[m] > array[start]){ std:: swap
(array[start], array[m]); } key = array[start]; int l = start; int r = end; while(l < r){ while(l < r && key < array[r]){ -- r; } std:: swap
(array[l], array[r]); while(l < r && key > array[l]){ ++ l; } std:: swap
(array[l], array[r]); } return l;}static void qsort( int* array, int start, int end){ if(start < end){ int p = partition(array, start, end); qsort(array, start, p-1); qsort(array, p+1, end); }}void quick_sort( int* array, int length){ qsort(array, 0, length-1);}int main(){ int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; quick_sort(array, 10); for(int i = 0; i != 10; ++i){ std::cout << array[i] << " "; } std::cout << std:: endl; return 0;}
In the best case, the time complexity of quick sorting is O (nlogn) and O (n2) in the worst case. To further improve the performance of quick sorting, it can also reduce unnecessary exchanges in the quick sorting method. The optimized code is as follows:
#include
#include static int partition( int* array, int start, int end){ //int key = array[start]; int key; int m = (start+end)/2; if(array[start] > array[end]){ std:: swap
(array[start], array[end]); } if(array[m] > array[end]){ std:: swap
(array[m], array[end]); } if(array[m] > array[start]){ std:: swap
(array[start], array[m]); } key = array[start]; int l = start; int r = end; while(l < r){ while(l < r && key < array[r]){ -- r; } //std ::swap
(array[l], array[r]); array[l] = array[r]; while(l < r && key > array[l]){ ++ l; } //std ::swap
(array[l], array[r]); array[r] = array[l]; } array[l] = key; return l;}static void qsort( int* array, int start, int end){ if(start < end){ int p = partition(array, start, end); qsort(array, start, p-1); qsort(array, p+1, end); }}void quick_sort( int* array, int length){ qsort(array, 0, length-1);}int main(){ int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; quick_sort(array, 10); for(int i = 0; i != 10; ++i){ std::cout << array[i] << " "; } std::cout << std:: endl; return 0;}
By now, the common sorting algorithms have basically been described, but in comparison, Merge Sorting is the most stable algorithm. If you have any questions, please leave a message to discuss them.
Link: http://blog.csdn.net/girlkoo/article/details/17606639
Author: girlkoo