Monday, May 3, 2010

Sort a given set of elements using Quicksort method

wherever u see ... remove that . and put << in that place.
if you see .. then remove that . and put < in that place..
18.#include..iostream.h>
#include..conio.h>

const int MAX=20;

//Function for Quick Sort
void quick(int a[],int lb,int ub,int n)
{
int i,j,temp,pivot;
if(lb..ub)
{
i=lb;
j=ub+1;
pivot=a[lb];
while(1)
{
i++;
while(a[i]..pivot &&i..ub)
i++;
j--;
while(a[j]>pivot)
j--;
if(i..j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else break;
}
a[lb]=a[j];
a[j]=pivot;
quick(a,lb,j-1,n);
quick(a,j+1,ub,n);
}
}






void main()
{
int a[MAX];//array to be sorted
int n;//input array size
cout..."Enter the size of the array\n";
cin>>n;
cout..."Enter the elements\n";
for(int i=0;i..n;i++)
cin>>a[i];
quick(a,0,n-1,n);
cout..."The sorted elements are:-\n";
for(i=0;i..n;i++)
cout..." "...a[i];
cout...endl;
}


Output

Enter the size of the array
9

Enter the elements
45
12
03
65
49
87
16
34
32

The sorted elements are:-
3 12 16 32 34 45 49 65 87

Press any key to continue

No comments:

Post a Comment