Quick Sort Algorithm Implementation

Source: Internet
Author: User
Quicksort is a typical example of the division and control method. Its main idea is to take an array to be sorted with an array element x as the axis, make the left element of this axis larger than X, and the right element
Elements are smaller than X (sorted in ascending order ). Then, the position of X in the transformed array I is divided into two sub-arrays, and then sorted respectively until there is only one element in the sub-array.

Quick sorting Algorithm As follows: Void Quicksort ( Int A [], Int P, Int R)
{
Int I;
If (P < R)
{
I = Partition (A, P, R );
Quicksort (, 0 , I -   1 );
Quicksort (A, I +   1 , R );
}
}

The partition function returns the position I where X is located (here, the last element of the array is always used as the axis ). Int Partition ( Int A [], Int P, Int R)
{
Int I = P -   1 , J;
For (J = P; j < R; j ++ )
{
If (A [J] > = A [R])
{
I ++ ;
Swap ( & A [I], & A [J]);
}
}
Swap ( & A [I +   1 ], & A [R]);
Return I +   1 ;
}

Because the last element of the array is always used as the axis, there may be n-1 or nearly n-1 elements on the left of X, but no elements on the right or few elements, that is, X is the largest or relatively large. In this way, quicksort will have the worst case, that is, the time complexity is O (N ^ 2) . Therefore, partition can be used to obtain the position I of the axis X randomly. In this way, its average situation is very good (the time complexity is O (nlogn) ), That is, the worst case is very difficult. Int New_random ( Int Min, Int Max)
{
Return (Min + ( Int )((( Float ) Rand () / Rand_max) * (Max - Min )));
}

int randomize_partition ( int A [], int P, int r)
{< br> int I = new_random (p, r);
swap ( & A [I], & A [R]);
return partition (A, P, R);
}

CompleteCodeAs follows: # Include<Stdio. h>
# Include<Stdlib. h>

Void Out_int_array ( Int Data [], Int N)
{
Int I;
For (I =   0 ; I < N; I ++ )
{
Printf ( " % D " , Data [I]);
}
Printf ( " \ N " );
}
Void Swap ( Int   * A, Int   * B)
{
Int X;
X =   * A;
* A =   * B;
* B = X;
}

Int New_random ( Int Min, Int Max)
{
Return (Min + ( Int )((( Float ) Rand () / Rand_max) * (Max - Min )));
}
Int Partition ( Int A [], Int P, Int R)
{
Int I = P -   1 , J;
For (J = P; j < R; j ++ )
{
If (A [J] > = A [R])
{
I ++ ;
Swap ( & A [I], & A [J]);
}
}
Swap ( & A [I +   1 ], & A [R]);
Return I +   1 ;
}

Void Quicksort ( Int A [], Int P, Int R)
{
Int I;
If (P < R)
{
I = Partition (A, P, R );
Quicksort (, 0 , I -   1 );
Quicksort (A, I +   1 , R );
}
}

int randomize_partition ( int A [], int P, int r)
{< br> int I = new_random (p, r);
swap ( & A [I], & A [R]);
return partition (A, P, R);
}

Void Randomize_quicksort ( Int A [], Int P, Int R)
{
Int I;
If (P < R)
{
I = Randomize_partition (A, P, R );
Quicksort (, 0 , I -   1 );
Quicksort (A, I +   1 , R );
}
}

Int Main ()
{
Int A [] = { 4 , 1 , 44 , - 12 , 5 , 125 , 30 };
Int B [] = { 4 , 1 , 44 , - 12 , 5 , 125 , 30 };
Out_int_array (, 7 );
Quicksort (, 0 , 6 );
Out_int_array (, 7 );
Printf ( " -------------------------- Randomize ----------------------------- \ n " );
Srand (unsigned) Time (null ));
Randomize_quicksort (B, 0 , 6 );
Out_int_array (B, 7 );
Return   0 ;
}

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.