//Program
#include<stdio.h>
void quicksort(int [10],int,int);
void disp(int [10],int);
int size;
int main()
{
int x[20],i;
//clrscr();
printf("\nEnter size of the array :");
scanf("%d",&size);
printf("\nEnter %d elements :",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("\nSorted elements :");
for(i=0;i<size;i++)
printf("%d\t",x[i]);
//getch();
return 0;
}
void disp(int x[10],int size)
{ int i;
for(i=0;i<size;i++)
printf("%d\t",x[i]);
}
void quicksort(int x[10],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
{ i++; printf("\n i=%d",i);
}
while(x[j]>x[pivot])
{
j--;
printf("\n j=%d",j);
}
if(i<j)
{
printf(" \nexchange x[i]<->x[j] %d <-> %d ", x[i],x[j]);
temp=x[i];
x[i]=x[j];
x[j]=temp;
}//end if
} //end while
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
printf("\npass: ");
disp(x,size);
quicksort(x,first,j-1);
printf("\nupper execute");
quicksort(x,j+1,last);
} //end if
}
// output:
Enter size of the array :7
Enter 7 elements :7
6
8
5
3
2
1
i=1
i=2
exchange x[i]<->x[j] 8 <-> 1
i=3
i=4
i=5
i=6
j=5
pass: 2 6 1 5 3 7 8
i=1
j=3
j=2
exchange x[i]<->x[j] 6 <-> 1
i=2
j=1
pass: 1 2 6 5 3 7 8
upper execute
i=3
i=4
pass: 1 2 3 5 6 7 8
i=3
j=2
pass: 1 2 3 5 6 7 8
upper execute
upper execute
upper execute
Sorted elements :1 2 3 5 6 7 8
--------------------------------