This C code recursively sorts an array of upto 20 source code

This snippet submitted by Abhishek H.Dwivedi on 2005-02-17. It has been viewed 30448 times.
Rating of 5.4 with 273 votes

 #include<stdio.h>
#include<conio.h>
int a[20],n,i,c=0;
void main()
{
void quick(int,int);
clrscr();
printf(\"Enter number of elements[Max. 20 \");
scanf(\"%d\",&n);
printf(\"Enter elements \");
for(int i=1;i<=n;i++)
scanf(\"%d\",&a[i]);
quick(1,n);
printf(\"\n\");
for(i=1;i<=n;i++)
printf(\"%d \",a[i]);
printf(\"\n %d\",c);
getch();
}
void quick(int x,int y)
{
int l,k,temp;
k=x;
l=y;
if((y-x)>=1)
{
while(l>=x)
{
if(a[k]>a[l]&&k<l)
{
c++;
temp=a[l];
a[l]=a[k];
a[k]=temp;
k=l;
l--;
}
else
if(a[k]<a[l]&&k>l)
{
c++;
temp=a[l];
a[l]=a[k];
a[k]=temp;
temp=k;
k=l;
l=temp;
}
else
l--;
}
quick(x,k-1);
quick(k+1,y);
}
}
 
 




More C and C++ source code snippets