Common sorting algorithms-quick sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.