Saturday, January 3, 2015

Quick Sort in c programming........

Quick Sort :

#include<stdio.h>
#include<conio.h>
int no;
void quicksort(int low,int high,int a[])
{
int i,down=low,up=high,pivot;
pivot=a[down];
down++;
if(low<high)
{
while(down<=up)
{
while(pivot>a[down])
   down++;
while(pivot<a[up])
   up--;
if(down<up)
{
   a[down]=a[down]+a[up];
   a[up]=a[down]-a[up];
   a[down]=a[down]-a[up];
}
}
a[low]=a[up];
a[up]=pivot;
printf("\nArray Status:");
for(i=0;i<no;printf("-->%d ",*(a+i)),i++);
quicksort(low,up-1,a);
quicksort(up+1,high,a);
}
else
return;
}
void main()
{
int *a;
int n,i;
printf("Enter the size of the array:");
scanf("%d",&n);
no=n;
a=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;scanf("%d",(a+i)),i++);
quicksort(0,n-1,a);
printf("\nSorted List:");
for(i=0;i<n;printf(" %d",*(a+i)),i++);
getch();
}



Copy and paste it in a notepad...then save it in "tc\bin" as "name.c" and open the program in turbo c compiler "by file->open->name.c"

No comments:

Post a Comment