Program to sort an array of integers in ascending order using quick sort.

#include<iostream>
#include<conio.h>
using namespace std;
int a[10],l,u,i,j;
void quick(int *,int,int);
int main()
{
cout<<"Enter 10 Elements\n";
for(i=0;i<10;i++)
cin>>a[i];
l=0;
u=9;
quick(a,l,u);
cout <<"\nSorted Elements\n";
for(i=0;i<10;i++)
cout<<a[i]<< " ";
cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
return 0;
}

void quick(int a[],int l,int u)
{
   int p,temp;
if(l<u)
{
p=a[l];
i=l;
j=u;
while(i<j)
{
   while(a[i]<=p&&i<j)
i++;
   while(a[j]>p&&i<=j)
   j--;
if(i<=j)
   {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[l];
a[l]=temp;
cout <<"\n";
for(i=0;i<10;i++)
cout <<a[i]<<" ";
quick(a,l,j-1);
quick(a,j+1,u);
}
}

Output:



Comments

Popular posts from this blog

To implement the various components of HTML5 Canvas

Program to illustrate the concept of templates.

Program to illustrate the order of execution of constructors and destructors in inheritance.