/*C Program Quick Sort*/
#include<stdio.h>
#include<conio.h>
int partition(int a[],int low,int high)
{
int i,j,temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high&&key>=a[i])
i++;
while(key<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
void quick_sort(int a[],int low,int high)
{
int j;
if(low<high)
{
j=partition(a,low,high);
quick_sort(a,low,j-1);
quick_sort(a,j+1,high);
}
}
void main()
{
int i,n,a[20];
printf(" enter no. of ele \n");
scanf("%d",&n);
printf("enter the ele \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("sorted list is \n");
for(i=0; i<n; i++)
printf(" %d \n",a[i]);
}
No comments:
Post a Comment