Quick sort (Swap sort)-eight big sort three big find totals (6)

Source: Internet
Author: User

Basic ideas

1. First, a number is taken from the series as the base number.

2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.

3. Repeat the second step for the left and right intervals until there is only one number for each interval.

Performance

Time complexity: The time complexity on average is O (Nlogn). The worst-case time complexity is O (N2).

Spatial complexity: In addition to the space consumption implemented by the program (for example, recursive stacks), the fast sorting algorithm consumes only a certain amount of space (i.e. O (1), constant-level space).

Stability: An unstable algorithm

Attention

Compiler function library comes with a quick sort function: qsort ()

Usage: void qsort (void *base, int nelem, int width, int (*fcmp) (const void *,const void *));

Parameters: 1 The number of elements to be sorted in the first address of the array 2 array 3 The space size of each element 4 pointers to functions

Code and analysis

Http://developer.51cto.com/art/201403/430986.htm

Initial sequence "6 1 2 7 9 3 4 5 10 8"

Find a number that is less than 6 from right to left, then find a number greater than 6 from left to right, then swap them. Here you can use two variables I and J, pointing to the leftmost and rightmost of the sequence, respectively. We have a nice name for these two variables "Sentinel I" and "Sentinel J". At first let Sentinel I point to the leftmost (ie i=1) of the sequence, pointing to the number 6. Let Sentinel J point to the far right of the sequence (that is, =10), pointing to the number 8.

First Sentinel J started out. Since the number of datums set here is the leftmost number, it is important to have Sentinel J go first. Sentinel J moves to the left (that is, j--) one step at a pace until a number less than 6 is found to stop. The Sentinel I then moves to the right (that is, i++), until a number greater than 6 is found to stop. The Last Sentinel J stopped in front of the number 5, and Sentinel I stopped at the number 7.

Now swap the values of the elements pointed to by Sentinel I and Sentinel J. The sequence after the interchange is as follows:

6 1 2 5 9 3 4 7 10 8

To this, the first exchange ends. Next, Sentinel J continues to move to the left (again, it must be the Sentinel J to start each time). He found 4 (smaller than the base number 6, met the requirements) and stopped. Sentinel I also continued to move to the right, and he found 9 (larger than the base number 6 to meet the requirements) and then stopped. At this point the interchange is exchanged again, followed by the following sequence:

6 1 2 5 4 3 9 7 10 8

  

The second exchange ends, "probing" continues. Sentinel J continued to move to the left, and he found 3 (smaller than the base 6, which met the requirements) and then stopped. Sentry I continues to move to the right, bad! At this point Sentinel I and Sentinel J met, Sentinel I and Sentinel J all walked to 3. Description the "Probe" ends at this point. We swapped the benchmark number 6 and 3. The sequence after the interchange is as follows:

3 1 2 5 4 6 9 7 10 8

  

To this first round of "probing" really ended. At this point, the base number 6 is the cutoff point, 6 left of the number is less than or equal to 6, 6 to the right of the number is greater than or equal to 6, then as long as the simulation just the method of 6 left and right of the sequence can be processed. Recalling the process just now, the mission of Sentinel J is to find a number less than the base number, and Sentinel I's mission is to find a number larger than the base number, until I and J meet.

The entire process is as follows:

The code is as follows:

1 //Quick Sort2 voidQuick_sort (intS[],intLintR)3 {4     if(L <R)5     {6         //Swap (S[l], s[(l + R)/2]);//swap this number and first number in the middle to see note 17         inti = l, j = r, x =S[l];8          while(I <j)9         {Ten              while(I < J && S[j] >= x)//find the first number less than x from right to left Onej--;  A             if(I <j) -s[i++] =S[j]; -              the              while(I < J && S[i] < x)//find the first number greater than or equal to x from left to right -i++;  -             if(I <j) -s[j--] =S[i]; +         } -S[i] =x; +Quick_sort (S, l, I-1);//Recursive invocation AQuick_sort (s, i +1, R); at     } -}

Quick sorting also has many improved versions, such as random selection of base numbers, which are sorted directly by another method to reduce the recursion depth when there is less data in the interval.

Quick sort (Swap sort)-eight big sort three big find totals (6)

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.