Quick Sort Method (i)

Source: Internet
Author: User

The fast sorting method (Quick Sort) is currently recognized as one of the fastest sorting methods (depending on the object of the solution), although the fast sorting method can reach O (N2) in the worst case, but in most cases, the efficiency of the fast sorting method is quite good.
The basic spirit of the fast sorting method is to find the proper axis in the sequence, then divide the series into two, and then order the left and right numbers respectively, and the efficiency of the fast sorting method is the axis choice. The first quick-sort version introduced here is the version mentioned in most textbooks, because it is the easiest to understand and best fits the concept of axis segmentation and left-to-right sequencing, which is suitable for beginners to explain. The quick calculus described here is as follows: Set the leftmost number to the axis and record its value as s
Compass Ring Processing:
Make index I look to the right from the left of the sequence until I find a number greater than s
Make index J look to the left from the left and right side of the sequence until it finds a number less than s
If I >= J, then leave the loop
If I < J, the value at the Exchange index I and J two
Swap the left axis with J
Recursive to the left of the axis
Recursive to the right of the axis
By the following algorithm, the value on the left side of the axis will be less than s, and the value on the right side of the axis will be greater than s, so the left and right sides of the axis
Recursion can be done for the purposes of sorting, such as the following instance, * representing the number to be exchanged, [] representing the axis:
[41] 24 76* 11 45 64 21 69 19 36*
[41] 24 36 11 45* 64 21 69 19* 76
[41] 24 36 11 19 64* 21* 69 45 76
[41] 24 36 11 19 21 64 69 45 76
21 24 36 11 19 [41] 64 69 45 76
In the example above, the value on the left side of 41 is smaller than it, and the value on the right is larger than it, so that it can be handed back to sort out
Yes.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
#define SWAP (x, y) {int t; t = x; x = y; y = t;}
void Quicksort (int[], int, int);
int main (void) {
int Number[max] = {0};
int i, NUM;
Srand (Time (NULL));
printf ("Before sorting:");
for (i = 0; i < MAX; i++) {
Number= rand ()% 100;
printf ("%d", number);
}
Quicksort (number, 0, MAX-1);
printf ("\ n sort after:");
for (i = 0; i < MAX; i++)
printf ("%d", number);
printf ("\ n");
return 0;
}
void quicksort (int number[], int left, int. right) {
int I, j, S;
if (left < right) {
s = number[left];
i = left;
j = right + 1;
while (1) {
Look right.
while (i + 1 < number.length && Number[++i] < s);
Look left.
while (J-1 >-1 && number[--j] > s);
if (i >= j)
Break
SWAP (number, Number[j]);
}
Number[left] = Number[j];
NUMBER[J] = s;
Quicksort (number, left, j-1); To the left hand back
Quicksort (number, j+1, right); To the right to be handed back
}
}
For more highlights, please follow: http://bbs.superwu.cn

Focus on the two-dimensional code of Superman Academy:

Follow the Superman college Java Free Learning Exchange Group:

Quick Sort Method (i)

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.