Quick sort

// Quick sort //

#include
#include
#define SIZE 10
int main()
{
void quick(int [],int,int,int);
clrscr();
int i,n,arr[SIZE];
cout<<"Enter the no. of terms:";
cin>>n;
cout<<"Enter the list:\n";
for(i=0;icin>>arr[i];
quick(arr,n,0,n-1);
cout<<"The sorted list is:\n";
for(i=0; i < n; i++)
cout << arr[i] << endl;
getch();
return(0);
}
void quick(int a[],int n,int lb,int ub)
{
void split(int [],int,int,int *);
int loc;
if (lb{
split(a,lb,ub,&loc);
quick(a,n,lb,loc-1);
quick(a,n,loc+1,ub);
}
return;
}
void split(int a[],int beg,int end,int *loc)
{
int left=beg,right=end,temp,i;
*loc=beg;
for(i=right;i>=left;i--)
{
while(a[*loc] <=a[right] && *loc!=right)
right=right-1;
if(a[*loc]>a[right])
{
temp=a[*loc];
a[*loc]=a[right];
a[right]=temp;
}
}
for(i=left; i < right; i++)
{
while(a[left]<=a[*loc]&&*loc!=left)
left=left+1;
if(a[left]>a[*loc])
{
temp=a[*loc];
a[*loc]=a[left];
a[left]=temp;
}
}
return;
}

Post a Comment

Previous Post Next Post