Sort Quick Sort

Source: Internet
Author: User
Tags benchmark

Fast sorting algorithm Quicksort
void QuickSort (Seqlist r,int low,int High)
{//To R[low. High] Quick Sort
int pivotpos;//position of the base record after dividing
if (LowPivotpos=partition (R,low,high);//To R[low. High] do divide
QuickSort (r,low,pivotpos-1);//left-interval recursive sequencing
QuickSort (R,pivotpos+1,high);//Right interval recursive ordering
}
}//quicksort

Attention:
To sort the entire file, just call Quicksort (r,1,n) to complete the r[l. N] the sort.


Partitioning algorithm partition
① Specific procedure
The first step: (initialization) set two pointers I and J, their initial values are the lower and upper bounds of the interval, namely I=low,i=high; Select the first record of the unordered area R[i] (ie R[low]) as the benchmark record, and save it in the variable pivot;
Step two: Let J scan from high to left until the 1th keyword is found to be less than Pivot.key Records R[J], the r[j] is moved to the position I refer to, which is equivalent to r[j] and the base R[i] (that is, pivot) exchanged, A record with a keyword less than the Datum key Pivot.key is moved to the left of the Datum, R[j] is equivalent to pivot, and then the I pointer is scanned to the right from the i+1 position until the 1th keyword is found to be greater than the Pivot.key record R[i], r[i Move to the position I refer to, this is equivalent to exchanging r[i] and datum r[j] so that the record of the keyword greater than the Datum key is moved to the right of the datum, and then the r[i] is equivalent to storing the pivot, and then the pointer J is shifted from position j-1 to the left, so alternating the scanning direction, From each end to the middle, until i=j, I is the benchmark pivot final position, will pivot placed in this position on the completion of a division.
② Partitioning algorithm:
int Partition (seqlist r,int i,int J)
{///call partition (R,low,high), on R[low ... High] do divide,
and returns the location of the base record
Recetype pivot=r[i];//using the 1th record of the interval as the benchmark '
while (I<J) {//From the interval ends alternately to the middle scan, until i=j until
while (I<j&&r[j].key>=pivot.key)//pivot is equivalent to position I
j--//right-to-left scan for 1th keyword less than pivot.key record R[j]
if (i<j)//= keyword found r[j] <pivot.key
R[I++]=R[J];//= Exchange R[i] and r[j], swap i pointer plus 1
while (I<j&&r[i].key<=pivot.key)//pivot is equivalent to position J.
i++//scan from left to right, find 1th keyword greater than Pivot.key record R[i]
if (I&LT;J)//means found r[i], so R[i].key>pivot.key
R[j--]=r[i]; Equivalent to Exchange R[i] and R[j], after the swap J pointer minus 1
}//endwhile
R[i]=pivot//Benchmark record has been last positioned
return i;
}//partition



Instance:

#include <stdio.h>

int part (int a[],int I,int j)
{
int pos=a[i];
while (I&LT;J) {
while (I<j&&a[j]>=pos) j--;
if (i<j) a[i++]=a[j];
while (I<j&&a[i]<=pos) i++;
if (i<j) a[j--]=a[i];
}
A[i]=pos;
return i;
}
void quicksort (int a[], int low, int high)
{
int POS;
if (LowPos=part (A,low,high);
Quicksort (A,LOW,POS-1);
Quicksort (A,pos+1,high);
}
}

int main ()
{
int a[10]={29,48,7,16,325,4,3,22,112,40};
int i;
Quicksort (a,0,9);

        printf ("The Out put is\n");
        for (i=0;i<10;i++)
         printf ("%d\n", A[i]);
        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.