Classic sorting algorithm-quick sort

Source: Internet
Author: User

Classic sorting algorithm-quick sort

Principle, through a scan of the data to be sorted into separate two parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence

As an example,

such as unordered array [6 2 4 1 5 9]

a)remove the first item [6] First,

Use [6] to compare the rest of the items in turn,

If the [6] is smaller than [6],2 4 1 5 are smaller than [6], so all put to the front of [6]

If compared to [6] large put [6] behind, 9 than [6] large, put to [6] behind,//6 out after a shout, than my small station front, bigger than my station behind, action!

After a trip, it becomes the following:

Sort before 6 2 4 1 5 9

After sort 2 4 1 5 6 9


b)proceed to the Ban La [2 4 1 5] for quick sequencing

Repeat step a) to change to the following:

Sort before 2 4 1 5

After sort 1 2 4 5

Before the ban La Sort is complete, the total sort is done as well:

Before sorting: [6 2 4 1 5 9]

After sorting: [1 2 4 5 6 9]

Sort End

The following code implementation is for reference only

        Static intPartitionint[] unsorted,intLowintHigh ) {            intPivot =Unsorted[low];  while(Low <High ) {                 while(Low < High && Unsorted[high] > Pivot) high--; Unsorted[low]=Unsorted[high];  while(Low < High && Unsorted[low] <= pivot) low++; Unsorted[high]=Unsorted[low]; } Unsorted[low]=pivot; returnLow ; }        Static voidQuick_sort (int[] unsorted,intLowintHigh ) {            intLOC =0; if(Low <High ) {Loc=partition (unsorted, Low, high); Quick_sort (unsorted, Low, loc-1); Quick_sort (unsorted, loc+1, high); }        }        Static voidMain (string[] args) {            int[] x = {6,2,4,1,5,9 }; Quick_sort (x,0, X.length-1); foreach(varIteminchx) {Console.WriteLine (item+",");        } console.readline (); }

Classic sorting algorithm-quick sort

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.