Classic Algorithm Series one-quick sort

Source: Internet
Author: User

Write a series of classic algorithms to learn together!

1. Quick Sort

Basic idea:

1. First remove a number from the series as the base number.

2. The partitioning process will place the number of digits above it to the right, less than or equal to its number to the left of it.

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

On the internet to see a blog, the quick sort of named digging pit Fill + divide the method, it feels very reasonable, so the program will be according to the two departments to write.

First of all, the idea of digging a hole:

1.i=l;j=r; Dig the cardinality to form the first pit a[i].

2.j--by the back forward to find a smaller number than it, find out after digging this number to fill the previous pit a[i].

3.i++ by the front and back to find a larger number than it, found after digging this number to fill the previous pit a[j].

4. Repeat the 2,3 step until the i==j, filling the cardinality into a[i].

Code implementation

intAdjust (inta[],intI,intj)

{
intx = A[i];
while(I<J)
{
while(i<j && x <= a[j])
j--;
if(I&LT;J)
{
A[i] = A[j];
i++;
}
while(i<j && x >= a[i])
i++;
if(I&LT;J)
{
A[J] = A[i];
j--;
}
}
A[i] = x;
returnI
}

Divide and Conquer:

intQuick_sort (inta[],intL,intR)

{
intI
if(L < R)
{
i = Adjust (a,l,r);
Quick_sort (a,l,i-1);
Quick_sort (a,i+1, R);
}
}

This is a two-step solution to the quick sort, quite simple. Of course this is a bit of a hassle, it can be simpler:

intQuick (inta[],intL,intR)

{
inti = l;
intj = r;
if(L < R)
{
intx = A[i];
while(I&LT;J)
{
while(i<j && x <= a[j])
j--;
if(I&LT;J)
{
A[i] = A[j];
i++;
}
while(i<j && x >= a[i])
i++;
if(I&LT;J)
{
A[J] = A[i];
j--;
}
}
A[i] = x;
Quick (a,l,i-1);
Quick (a,i+1, R);
}
}

http://www.cnblogs.com/my-life/category/425361.html

Classic Algorithm Series one-quick sort

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.