Algorithm--Quick sort

Source: Internet
Author: User

Quick Sort

Algorithmic thinking

The idea of quick sorting is the thought of division and treatment.

A quick sort is to find an element (which can theoretically be arbitrarily found) as a datum (pivot), and then partition the array so that the values of the elements on the left of the datum are not larger than the datum, and the element values to the right of the datum are not less than the datum values, so that the elements of the datum are adjusted to the correct position after Recursive quick sorting, and other n-1 elements are also adjusted to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sort is complete. So the core algorithm of the fast sorting algorithm is the partitioning operation, that is, how to adjust the position of the datum and adjust the final position of the return datum to divide and conquer the recursion.

For example, this may not be too good to understand. Suppose that the order to sort is listed as

2 2 4 9 3 6 7 1 5 First use 2 as the benchmark, using the I-j two pointers are scanned from both sides, separating the elements smaller than 2 and the elements larger than 2. First compare 2 and 5, 5:2, J shift left

2 2 4 9 3 6 7 1 5 compare 2 and less than 2, so put 1 in the position of 2

2 1 4 9 3 6 7 1 5 Compare 2 and bis more than 2, so move 4 to the back

2 1 4 9 3 6 7 4 5 Compare 2 and7,2 and 6,2 and 3,2 and 9, all greater than 2, meet the conditions, and therefore unchanged

After the first round of quick sorting, the elements change to look like this

[1] 2 [4 9 3 6 7 5]

After that, the 2 left side of the element is fast, because there is only one element, so the end of the fast line. The right side of the queue, recursive, and eventually produce the final result.

Code

voidQuickSort (intLeftintRight ) {    if(left >= right)return; intLow =Left ; intHigh =Right ; intKey =V[left];  while(Low <High ) {         while(Low < High && V[high] > key) high--; V[low]=V[high];  while(Low < High && V[low] < key) low++; V[high]=V[low]; } V[low]=key; QuickSort (left, low-1); QuickSort ( Low+1, right);}

The worst time for fast sorting is O (N2), but in terms of average performance it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NLGN).

Example:

#include <iostream>#include<vector>using namespacestd;intv[Ten] = {5,3,9,8,1,2,7,6,4,0 };voidQuickSort (intLeftintRight ) {    if(left >= right)return; intLow =Left ; intHigh =Right ; intKey =V[left];  while(Low <High ) {         while(Low < High && V[high] > key) high--; V[low]=V[high];  while(Low < High && V[low] < key) low++; V[high]=V[low]; } V[low]=key; QuickSort (left, low-1); QuickSort ( Low+1, right);}intMain () {QuickSort (0,9);  for(inti =0; I <Ten; i++) cout<< V[i] <<" "; cout<<Endl; return 0;}

Results:

0 1 2 3 4 5 6 7 8 9

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.