Tuesday, August 25, 2009

quick sort

#include"stdio.h"
#include"conio.h"
void quick(int a[],int first,int last);
void swap(int a[],int i,int j);
int i,j,temp,a[20],n,pivot;
void main()
{
clrscr();
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
printf("enter the nos for sorting\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
printf("%d",a[i]);


quick(a,0,n-1);

printf("the sorted list\n");
for(j=0;j<n;j++)
printf("\n %d",a[j]);
getch();
}


void quick(int a[],int first,int last)
{
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{

while(a[i]<=pivot&&ii++;

while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}


swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}

void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

No comments:

Post a Comment