#include<stdio.h>
#include<conio.h>
void quick_sort(int [],int,int);
int partition(int [],int,int);
int a[100],i,j,n,temp,lb,ub,start,end,pivot,loc;
void main(){
// int a[100],i,n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("After Sorting: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void quick_sort(int a[],int lb,int ub){
if(lb<ub){
loc=partition(a,lb,ub);
quick_sort(a,lb,loc-1);
quick_sort(a,loc+1,ub);
}
}
int partition(int a[],int lb,int ub){
pivot=a[lb];
start=lb;
end=ub;
while(start<end){
while(a[start]<=pivot)
start++;
while(a[end]>pivot)
end--;
if(start<end){
//swap(a[start],a[end])
temp=a[start];
a[start]=a[end];
a[end]=temp;
}
}
//swap(a[lb],a[end])
temp=a[lb];
a[lb]=a[end];
a[end]=temp;
return end;
}
No comments:
Post a Comment