Quick Sort and Merge sort (C language)

Source: Internet
Author: User

1.0 Quick Sort algorithm

(1) Decomposition (2) recursive solution (3) merging

int partition (int a[],int p,int R)

{

int i=p,j=r+1;

int x=a[p];

int temp;

while (1)//swap elements of <x to the left element, >x elements swap to the right element

{

while (a[++i]<x && i<r);

while (A[--J]>X);

if (I>=J)

Break

Temp=a[i];

A[I]=A[J];

A[j]=temp;

}

A[P]=A[J];

A[j]=x;

Return J;

}

void QSort (int a[],int p,int r,int num)

{

int i;

if (p<r)

{

int q=partition (A,P,R);

QSort (A,p,q-1,num); Sort the left half of the paragraph

QSort (A,q+1,r,num); Sort the right half of the paragraph

}

printf (" Quick sort process:");

for (i=0;i<num;i++)

{

printf ("%d", a[i]);

}

printf ("\ n");

}

int main ()

{

int num;

int i;

int a[100];

printf (" Please enter array length:");

scanf ("%d", &num);

printf (" please input array:\ n");

for (i=0;i<num;i++)

{

scanf ("%d", &a[i]);

}

QSort (A,0,num-1,num);

printf (" quick sort Result:");

for (i=0;i<num;i++)

{

printf ("%d", a[i]);

}

return 0;

}

2.0 Merge Sort algorithm

Combine two ordered left and right sub-tables (in mid) into an ordered table

void merge (int a[],int first,int mid,int last)

{

int Indexa=first;

int indexb=mid+1;

int tempindex=0;

int i;

static int temp[1000];

while (Indexa<=mid && indexb<=last)//to traverse the left and right sub-tables, if one of the sub-tables is traversed, jump out of the loop

{

if (A[indexa]<a[indexb])

{

Temp[tempindex++]=a[indexa++];

}

Else

{

Temp[tempindex++]=a[indexb++];

}

}

    Once a child table has been traversed, jump out of the loop and put the remainder of the other side of the child table into the staging array (ordered)

while (Indexa<=mid)

{

Temp[tempindex++]=a[indexa++];

}

while (Indexb<=last)

{

Temp[tempindex++]=a[indexb++];

}

Writes an ordered sequence of columns in the staging array to the set position of the target array, making the sorted array segments orderly

tempindex=0;

for (i=first;i<=last;i++)

{

A[i]=temp[tempindex++];

}

}

void mergesort (int a[],int first,int last,int num)

{

int i;

if (first<last)//The length of the child table is greater than 1, then go to the following recursive processing

{

int mid= (first+last)/2;

MergeSort (A,first,mid,num);

MergeSort (A,mid+1,first,num);

Merge (A,first,mid,last);

}

printf (" array at merge sort:\ n");

for (i=0;i<num;i++)

{

printf ("%d", a[i]);

}

printf ("\ n");

}

int main ()

{

int num;

int i;

int a[100];

printf (" Please enter array length:");

scanf ("%d", &num);

printf (" please input array:\ n");

for (i=0;i<num;i++)

{

scanf ("%d", &a[i]);

}

MergeSort (A,0,num-1,num);

printf (" merge sorted array:\ n");

for (i=0;i<num;i++)

{

printf ("%d", a[i]);

}

return 0;

}

Quick Sort and Merge sort (C language)

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.