Quick Sort | C Language | Design Analysis and Algorithum
Quick Sort:
About:
Quick Sort is a Divide and Conquer algorithm. A
pivot and partition element in the given array rounded and picked the pivot.Developed by British
computer scientist Tony Hoare in 1959, published in 1961, but is still used for sorting and faster than its main competitors, merge sort
and heap sort.
Code:
#include<stdio.h>
int a[20];
void main()
{
int n,i;
printf("Enter the size of the array:\n");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(0,n-1);
printf("Array after sorting\n");
show(0,n);
}
quick_sort(int l,int u)
{
int q;
if(l<u)
{
q=part(l,u);
quick_sort(l,q-1);
quick_sort(q+1,u);
}
}
int part(int l,int u)
{
int x,i,j,t;
x=a[u];i=l-1;
for(j=l;j<u;j++)
{
if(a[j]<=x)
{
i++;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
t=a[i+1];
a[i+1]=a[u];
a[u]=t;
return i+1;
}
int show(int l,int u)
{
int i;
for(i=l;i<u;i++)
printf("%d",a[i]);
printf("\n");
}
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)