C-language implementation of array fast sorting (including detailed explanation of the algorithm)

Source: Internet
Author: User
Tags rounds

/*Description: The code refers to the online code, but the analysis for the individual original, this sticker weight in the description of the idea of fast sorting algorithm and operation process. */Code section: #include<stdio.h>#include<stdlib.h>voidQuickSort (int* Arr,intStartpos,intendpos) {  intI, J; intkey; Key=Arr[startpos]; I=startpos; J=Endpos;  while(i<j) { while(Arr[j] >= key && i<j)--j;//———— 1 sweep from behind until a a[j]<key is found or traversedArr[i] =Arr[j];  while(Arr[i] <= key && i<j) ++i;//———— 2 sweep from behind until a a[i]>key is found or traversedARR[J] =Arr[i]; } Arr[i]=key; ifI1>startpos) QuickSort (arr, startpos, I-1);//———— 1 If there are two or more numbers in front of key, the number before the key (there is one that will not be lined up naturally) if(Endpos>i +1) QuickSort (arr, i +1, Endpos);//———— 2 If there are two or more digits after key, the number after the key}intMain () {inta[ One], I;  for(i =0; i< One; i++) scanf_s ("%d", &A[i]); QuickSort (A,0,Ten);  for(i =0; i< One; i++) printf ("%d", A[i]); printf ("\ n"); return 0; } Parsing section:/*With array a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0 2 32 39 23 45 3 6 57 14 27 39 As an example, the implementation mechanism of the core Code first round: First enter quicksort (A, 0, 10); KEY=0,I=0,J=10, enters the outer while, enters the first inner layer while, because 0 is the smallest in the array, therefore J has swept to the head, j=0,arr[0] = arr[0]=0; Obviously cannot enter the second inner layer while, because i=j= 0, ends the outer while, executes the a[0]=key=0; obviously does not enter the first if, enters the second if, executes Quicksort (A, 1, 10), performs a sort from a[1] to a[10], the first round ends. The second round of execution quicksort (A, 1, ten), key=2,i=1,j=10, into the outer while, into the first inner while, because 2 is the smallest in the incoming segment, so J always sweep the head, j=1,arr[1] = arr[1]=2; Unable to enter the second inner layer while, due to i=j=1, ends the outer while, executes a[1]=key=2; obviously does not enter the first if, enters the second if, executes Quicksort (A, 2, 10), performs a sort from a[2] to a[10], the second round ends. Third round: Executes Quicksort (A, 2, ten), key=32,i=2,j=10, enters the outer while, enters the first inner layer while,a[10]=39>key=32,--j,j becomes 9;a[9]=27<key=32, Exit first inner while, execute a[i]=a[2]=a[j]=a[9]=27, array becomes a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] A[9 ] 0 2 27 39 23 45 36 57 14 27 39 Note at this time the value of a[j] (i.e. a[9]) is deposited in a[i] (i.e. a[2]), a[j] can be Assignment, 32? 32, why not? Note 32 is always saved by key, don't worry.Notice at this time i=2,j=9, the program executes sequentially, satisfies the second inner layer while, enters, starts from left to right sweep. A[2]=27<key=32,++i,i becomes 3 (note that this is inevitable, because the first a[i] is given by the last inner while A[j], and the condition given is a[j] (that is, A[i here)) <key); a[i]=a[3]=39 >key=32, execute A[j] (as already said, at this point the value of a[j]=a[9] is saved to a[2], A[j] can be modified) =a[9]=a[3] = 39, the array becomes a[0] a[1] a[2] a[3] a[4] a[ 5] a[6] a[7] a[8] a[9] a[10] 0 2 27 39 23 45 36 57 14 39 39 Note at this time a[i] =A[3] becomes modifiable. Note that at this point the i=3<j=9 does not jump out of the outer while. Continue execution of the first inner layer while,a[j]=a[9]=39>key=32,--j,j to 8 (which is also necessary to analyze with the front); a[j]=a[8]=14<key=32, exit the first inner while, execute a[i]=a[3]      =A[J]=A[8]=14, array changed to A[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0 2 27 14 23 45 36 57 14 39 39 Note at this point a[j]=a[8] becomes modifiable. At this time i=3,j=8, the program executes sequentially, satisfies the second inner layer while, enters, starts from left to right sweep. A[i]=a[3]=14<key=32,++i,i becomes 4 (inevitable), A[i]=a[4]=23<key=32,++i,i becomes 5;a[i]=a[5]= 45>key=32, exits second inner while, executes A[J] =A[8]=A[I]=A[5]=45, array changed to A[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0 2 27 14 23 45 36 57 45 39 39 Notice that A[I]=A[5] can be modified, and i=5,j=8, does not jump out of the outer while. Continue execution of the first inner layer while,a[j]=a[8]=45>key=32,--j,j to 7 (inevitable); a[j]=a[7]=57>key=32,--j,j to 6;a[j]=a[6]=36>key=32,--J, J becomes 5 Note that at this point i=j=5, exit three while.     Execute a[i]=a[5]=key=32, array becomes a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0  2 27 14 23 32 36 57 45 39 39 Note that the number before a[5] is less than key=32,a[5] is greater than key=32, and startpos=2,endpos=10. Obviously two if are satisfied, (this is a one-time judgment, the computer can only judge the first if, and so on when the program returns to this round to judge the second if, we make a one-time judgment is to illustrate the convenience) first into the first if, the execution of Quicksort (A, 2, 4), a[5] in front of the a[2] To A[4] (a[0],a[1] is lined up after the second round), into the next round (fourth round), but the third round is not over because the computer has not yet judged the second if. Fourth round: Execution of Quicksort (A, 2, 4), key=a[2]=27,i=2,j=4,startpos=2,endpos=4. Enter outer while, enter first inner layer while,a[j]=a[4]=23<key=27, execute a[i]=a[2]=a[j]=a[4]=23, array becomes a[0] a[1] a[2] a[3] a[4] a[ 5] a[6] a[7] a[8] a[9] a[10] 0 2 23 14 23 32 36 57 45 39 39 at this time A[j]=a [4] can be modified, and i=2,j=4, the program executes sequentially, enters the second while,a[i]=a[2]=23<key=27,++i,i to 3 (inevitable): A[i]=a[3]=14<key=27,++i,i becomes 4, notices i= j=4, exits all loops, executes a[i      ]=a[4]=key=27, array changed to A[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0 2 23 14 27 32 36 57 45 39 39 at this time i=j=4,startpos=2,endpos=4, it is obvious that the first if does not satisfy the second if (key after countless), so the execution Quickso RT (A, 2, 3) (row a[4] before the next round (fifth round), but the fourth round is not finished, because the computer has not yet judged the second if. Fifth round: Execution quicksort (A, 2,3), key=a[2]=23,i=2,j=3,startpos=2,endpos=3. Enter outer while, enter first inner layer while,a[j]=a[3]=14<key=23, execute a[i]=a[2]=a[j]=a[3]=14, array becomes a[0] a[1] a[2] a[3] a[4] a[ 5] a[6] a[7] a[8] a[9] a[10] 0 2 14 14 23 32 36 57 45 39 39 at this time A[j]=a                [3] can be modified, and i=2,j=3, the program executes sequentially, into the second while,a[i]=a[2]=14<key=23,++i,i into 3 (inevitable): Notice i=j=3, exit all loops, execute a[i]=a[3]= key=23, the array becomes A[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8 [a[9] a[10] 0 2 14 23 27 32 3 6 57 45 39 39 at this time i=j=3,startpos=2,endpos=3, obviously not satisfied with the first if, then judge the second, also not satisfied, so the fifth round end, returned to the previous round (fourth round) The second if, has been analyzed before, not meet the second if, so the fourth round end, Return to the second if of the previous round (third round) (until this point, the element before the final a[i]=a[5]=key=32 of the third round is lined up), this time satisfied. Execute Quicksort (A, 6, 10), a[5] After the element, into the next round (recorded as the fourth * round, in order to be different from the fourth round above, but also to reflect the relationship between the two). End of the third round. IV * Round: Execution quicksort (A, 6, ten), key=a[6]=36,i=6,j=10,startpos=6,endpos=10. At this time a[j]=a[3] can be modified, and i=2,j=3, the program executes sequentially, into the second while,a[i]=a[2]=14<key=23,++i,i to 3 (inevitable): Notice i=j=3, exit all loops, execute a[i]=a[3]= a[j] =a[10]=39>key=36,--j,j into 9;a[j]=a[9]=39>key=36,--j,j to 8;a[j]=a[8]=45>key=36,--j,j to 7;a[j]=a[7]=57> key=36,--j,j to 6; notice i=j=6, exit all while, execute a[i]=a[6]=key=36, array unchanged. At this time i=j=6,startpos=6,endpos=10. Obviously does not satisfy the first if, satisfies the second if, executes Quicksort (A, 7, 10) (the element after row A[6], enters the fifth * round, the fourth * round ends. V * Round: Executive quicksort (A, 7, ten), key=a[7]=57,i=7,j=10,startpos=7,endpos=10.  a[j]=a[10]=39<key=57, execute a[i]=a[7]=a[j]=a[10]=39, array becomes a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] A[9] a[10] 0 2 14 23 27 32 36 39 45 39 39 at this time i=7,j=10, the program executes sequentially, into the second inner layer Whil e,a[I]=a[7]=39<key=57,++i,i becomes 8 (inevitable), A[i]=a[8]=45<key=57,++i,i becomes 9;a[i]=a[9]=39<key=57,++i, I becomes 10; notice i=j= 10, exit all while, execute a[i]=a[10]=key=57.    Array changed to A[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0 2 14 23 27 32 36 39 45 39 57 at this time i=j=10,startpos=7,endpos=10. Obviously satisfies the first if, does not satisfy the second if (a[i]=a[10] has no elements behind), executes Quicksort (A, 7, 9) (row a[10] before the element), enters the sixth * wheel, but does not exit the fifth * wheel, because the computer has not yet judged the second if. The six rounds: Executes Quicksort (A, 7, 9), key=a[7]=39,i=7,j=9,startpos=7,endpos=9. a[j]=a[9]=39=key=39,--j,j changes to 8;a[j]=a[8]=4>key=39,--j,j to 7; notice i=j=7, exit all while, execute a[i]=a[7]=key=39, array unchanged. At this time i=j=7,startpos=7, endpos=9. Apparently not satisfied with the first if (a[i]=a[7] no element before), satisfies the second, executes Quicksort (A, 8, 9) (row a[7] after the element), into the seventh * round, the end of the six rounds. Seventh * Round: Executive quicksort (A, 8, 9), key=a[8]=45,i=8,j=9,startpos=8,endpos=9. A[j]=a[9]=39<key=45, execute a[i]=a[8]=a[j]=a[9]=39, array becomes a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] A [9] a[10] 0 2 14 23 27 32 36 39 39 39 39  At this time i=8,j=9, the program executes sequentially, into the second inner layer while,a[i]=a[8]=39<key=45,++i,i becomes 9 (inevitable); notice i=j=9, exit all while, and execute a[i]=a[10]=key=57.    Array changed to A[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] 0 2 14 23 27 32 36 39 39 45 57 at this time i=j=9,startpos=8,endpos=9, obviously does not meet the first if, then judge the second, also not satisfied, the seventh * round end. The program returns to the second if of the third round, the front has been analyzed, not satisfied, so the end of the five rounds. At this point, the entire quicksort function ends, and the array is lined up as shown above.*/If you feel that analysis too much, do not want to see, we recommend that you follow the code manually, it will be enlightened. Of course, the process can be combined and referenced under the analysis. The code has been tested to run successfully on VS2013! This article has two main purposes:1To exchange experiences with people for reference. 2In the next rookie, the code is inevitably inappropriate, begged the great God to criticize. Your criticism is at the beginning of the improvement, for your criticism, will be greatly appreciated!

C-language implementation of array fast sorting (including detailed explanation of the algorithm)

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.