OneCompiler

Lab_2

141

#include <stdio.h>
void quicksort(int [], int, int);
int partition(int [], int, int);
void main()
{
int a[20],n,i;
clrscr();
printf("enter the no of elements");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n original list");
for(i=0;i<n;i++)
printf("%4d",a[i]);
quicksort(a,0,n-1);
printf("\n sorted list");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
}
void quicksort(int A[], int low, int high)
{
int j;
if (low > high)
return;
else
j= partition(A,low,high);
quicksort(A, low, j-1) ;
quicksort(A,j+1,high);
}
int partition(int a[], int low, int high)
{
int pivot,i,j,temp;
pivot=a[low];
i=low;
j=high;
while(i<j)
{
while (a[i]<= pivot && i <=high)
i = i+1;
while (a[j] > pivot && j > low)
j = j - 1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
a[low]=a[j];
a[j]=pivot;
return(j);
}