//WRITE A C PROGRAM TO IMPLEMENT QUICK SORT TECHNIQUE
#include<stdio.h>#include<conio.h>
void quick_sort(int[],int,int);//quicksort prototype
void swap(int *x,int *y)
{
int t=*x;
*x=*y;
*y=t;
}
void main()
{
int a[10],n,i;
clrscr();
//reading number of elements in unsorted list
printf("\nEnter n value : ");
scanf("%d",&n);
//reading array elements
printf("\nEnter %d numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
//calling quick sort with array and starting and last indices
quick_sort(a,0,n-1);
//displaying sorted array elements
printf("\nSorted list\n");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
}
void quick_sort(int a[],int low,int high)
{
int i=low+1,j=high,key=low,t;
if(low>=high)
return;
while(i<=j)
{
while(a[i]<=a[key]) i++;
while(a[j]>a[key]) j--;
if(i<j)
swap(&a[i],&a[j]);
}
swap(&a[j],&a[key]);
//splitting unsorted list into two lists based on j position
quick_sort(a,low,j-1);
quick_sort(a,j+1,high);
}
Output
No comments:
Post a Comment