Nothing to do, brush up on the quick sorting method

Source: Internet
Author: User
Tags benchmark

Quick Sort method, still very common. Whether it's interviewing or writing code. Here's how to coding the quick Sort method. As to what the complexity is, please refer to Http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F#C.

The core of the fast sorting method is the Division method (Divide and Conquer), which is to divide an array into two and divide it into four. Until it is not divided, it is finished. So the quick Sort method is how to divide the problem. What's the point? Of course, according to the size of the points. So the ascending step of the quick Sort method is described as:

    1. Select a Datum element
    2. Swaps an array element. Put less than the benchmark value to the left, greater than the benchmark value on the right side, equal to the laissez-faire. Ensure that the datum element is the position of the split line.
    3. Use the left and right elements of the datum values as sub-arrays (all excluding datum elements), repeating the two steps above until the number of array elements is less than 2

Now the C + + code is drawn:

#include <iostream>using namespacestd;#defineMax_array 10/** * @brief divide split array * @param list * @param length * @return Split position * Divide the array by the first element of the array, * the left side is less than the datum, the right is greater than the datum * to move the split line method implementation points Cut*/intDivideint*list,intlength) {    intDivide_line =0;/*Default Split Line*/    intPivot = list[0];/*default selects first as Datum*/Swap (list[0],list[length-1] );/*Put the benchmark in the end so that the last one will never be exchanged*/     for(inti =0; i < Length;i + + )    {        if(List[i] < pivot)/*less than datum, to be placed to the left of the split line*/{swap (list[i],list[divide_line]); /*Exchange*/            /*elements that are smaller than the base grow, and the split line shifts right to ensure that the right side of the split line (including the split line) is not less than the Datum*/Divide_line++; }} swap (List[divide_line],list[length-1] );/*swap the last one to the split line to ensure that the split line value is the base value*/    returnDivide_line;}/** * @brief Conquer sort sub-array * @param list * @param length * Splits the array and sorts the pairs of arrays*/voidConquer (int*list,intlength) {    if(Length <=1)//decomposed into an element that is no longer decomposed        return; intDivide_line = Divide (list,length);/*Get Split Line*/Conquer (List,divide_line); //Array to the left of the sortConquer (list+divide_line+1, Length-divide_line-1);//array to the right of the sort, datum values no longer need to be sorted}/** * @brief quick_sort Fast Sorting method * @param list * @param length * Fast sorting method*/voidQuick_sort (int*list,intlength) {Conquer (list,length);}intMain () {intList[max_array] = {9,0,1,3,5,8,4,6,2,7 };    Quick_sort (List,max_array);  for(inti =0; i < Max_array;i + +) {cout<< List[i] <<" "; } cout<<Endl; return 0;}

Of course, this is just an original version of the algorithm designed to illustrate the fast sorting method. Of course there are more advanced versions, more optimized versions. In general, I'm not going to write a quick-sort method myself. Now the libraries of C and C + + already contain a large number of sorting algorithms.

C in the fast-sorting method Linux under the man qsort can see the usage and use cases.

Nothing to do, brush up on the quick sorting method

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.