#include<stdio.h>
void quick(int a[],int low,int high);
int partition(int a[],int low,int high);
int main()
{
int a[100],n,i,low,high;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
low=0;
high=n-1;
quick(a,low,high);
printf("The sorted array is:\n");
for(i=0; i<n; i++)
{
printf("%d\n",a[i]);
}
return 0;
}
void quick(int a[],int low,int high)
{
int j;
if(low<high)
{
j=partition(a,low,high);
quick(a,low,j-1);
quick(a,j+1,high);
}
}
int partition(int a[],int low,int high)
{
int key,i,j,t;
key = a[low];
i=low+1;
j=high;
do
{
while(i<=high && key>a[i])
{
i++;
}
while(j>=low && key<a[j])
{
j--;
}
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
} while(i<=j);
t=a[low];
a[low] = a[j];
a[j]=t;
return j;
}
Note: Need to be arranged in compiler after copied
OutPut:
Enter the size of the array: 5
Enter the elements: 9
4
3
0
5
The sorted array is:
0
3
4
5
9