Quick sorting)

Source: Internet
Author: User

AlgorithmReview.

Time Complexity:

Average O (N * log2n)

Worst case O (n2)

Preferably O (N * log2n)

Ideas:

Set the array to be sorted as A, with the length of N, starting with I = 0, j = n-1. For each round of fast sorting:

1. from the right to the left (j -- direction), find a value smaller than the key (generally set to a [0, then exchange a [I] with a [J.

2. Search for a value greater than the key from the left to the right (I ++ direction) and exchange a [I] with a [J.

3. Perform steps 1 and 2 until I> J. At this time, the left side of the key is a smaller value than the key, and the right side of the key is a greater value than the key. Then, the left half edge array and Right Half Edge array are recursion respectively. Stop recursion until the recursion termination condition (I> = J) is met.

Code:

# Include <iostream> Using   Namespace  STD;  /*  Output array elements in the specified range */  Void Showarray ( Int Arr [], Int Start, Int  End ){  For ( Int I = start; I <= end; I ++ ) {Cout <Arr [I] < "   "  ;} Cout < Endl ;}  /* Swap the values of elements in the array at the specified position  */  Void Swap ( Int Arr [], Int A, Int  B ){  Int Temp = 0  ; Temp = Arr [a]; arr [A] = Arr [B]; arr [B] = Temp ;}  /* Quick Sort Function  */  Void Sortunit ( Int Arr [], Int I, Int  J ){  If (I> = J ){  Return  ;}  Int Key = Arr [I];  Int Start = I; Int End = J;  Int Temp = 0  ;  While (I < J ){  While (ARR [J]> = Key & J> I) {J -- ;}  If (ARR [J] < Key) {swap (ARR, I, j );}  While (ARR [I] <= Key & I < J) {I ++ ;}  If (ARR [I]> Key) {swap (ARR, I, j );}}  Int Center = (I + J )/ 2  ;  /*  Auxiliary Function, check whether the output data is correct  */  Showarray (ARR, start, end); cout < "  Center Index = " <Center < "  , Center value =  " <Arr [center] <Endl < Endl;  /*  Recursive quick left half  */  Sortunit (ARR, start, center - 1  );  /*  Recursive fast right half side  */  Sortunit (ARR, center + 1  , End );}  Void  Main (){  Int N = 20 ; //  Set the length of the test array to 20      Int Arr [ 20 ] = { 0 }; //  Initialize an array      For ( Int I =0 ; I <n; I ++ ) {CIN > Arr [I];} sortunit (ARR,  0 , N- 1  );  //  View final result Showarray (ARR, 0 , N- 1  );} 

Code test:

Test data:

Use MATLAB to randomly generate a 1*20 vector in the range of (1,100 ).

> A = Randi ([1,100)

Vector:

66 4 85 94 68 76 75 40 66 18 71 4 28 5 10 83 70 32 96 4

Output:

 

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.