C Language Learning _ sequencing _ quick sorting

Source: Internet
Author: User

Quick sorting:

The basic idea of this method is:
1. First, extract a number from the series as the reference number.
2. In the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.
3. Repeat the second step for the left and right intervals until each interval has only one number.

Further mining of quick sorting involves digging holes and partitioning. This method is more practical and simple:

Summarize the number of pitfalls
1. I = L; j = R; dig out the reference number to form the first pit a [I].
2. j -- find a smaller number than it in the back and forward, and dig out the number in the previous pit a [I.
3. I ++ finds a larger number from the front to the back, and then mines the number to fill in the previous pit a [j.
4. Repeat steps 2 and 3 until I = j and enter the baseline number in a [I.

It is easy to get the code:

# Include
# Include


Void quick_sort (int s [], int l, int r );
Int main (void)
{
Int a [10] = {20, 55 };
Int I;


Quick_sort (a, 0, 9 );
For (I = 0; I <10; I ++)
Printf ("% d", a [I]);


Return 0;
}
// Quick sorting
Void quick_sort (int s [], int l, int r)
{
If (l <r)
{
// Swap (s [l], s [(l + r)/2]); // exchanges the number in the middle with the first number.
Int I = l, j = r, x = s [l];
While (I <j)
{
While (I <j & s [j]> = x) // find the first number less than x from the right to the left.
J --;
If (I <j)
S [I ++] = s [j];

While (I <j & s [I] <x) // from left to right find the first number greater than or equal to x
I ++;
If (I <j)
S [j --] = s [I];
}
S [I] = x;
Quick_sort (s, l, I-1); // recursive call
Quick_sort (s, I + 1, r );
}
}



Related Article

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.