Google Ads

Monday, May 25, 2009

QUICK SORT

#include “stdio.h”
#include “conio.h”
void quick(int a[20],int first,int last);
void swap(int a[20],int i,int j);
void main()
{
int n,i,a[20];
clrscr();
printf("\n\t\t\tQUICK SORT");
printf("\n\t\t\t-------------------");
printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the array elements:");
for(i=0;i scanf("%d",&a[i]);
quick(a,0,n-1);
printf("\n Sorted List are:\n");
for(i=0;i printf("%d ",a[i]);
getch();
}

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

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

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

}

Output will be

Enter the value of n:4

Enter the array elements:
29
80
43
56

Sorted List are:
29
43
56
80

No comments:

Post a Comment