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<J)
{
A[i] = A[j];
i++;
}
while(i<j && x >= a[i])
i++;
if(I<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<J)
{
while(i<j && x <= a[j])
j--;
if(I<J)
{
A[i] = A[j];
i++;
}
while(i<j && x >= a[i])
i++;
if(I<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