This Post Contains A C++ Program To Perform Quick Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Quick Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program To Perform Quick Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
int dim, a[10];
void qsort(int, int);
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int i, l, r;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
l=0;
r=dim-1;
qsort(l, r);
cout<<" \n After Quick Sort : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* Source Code For Computing Quick Sort */
void qsort(int left, int right)
{
int i, j, p, temp;
if(right>left)
{
i=left;
j=right;
p=a[left];
while(right>left)
{
do
{
i++;
}
while( (a[i]<=p) && (i<=right) );
while( (a[j]>=p) && (j>left) )
{
--j;
}
if(j>i)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[left];
a[left]=a[j];
a[j]=temp;
qsort(left, j-1);
qsort(i, right);
}
}
/* End Of Main Program */
Output :
Enter Dimension : 5
Enter Values For Array :
3 1 5 2 4
After Quick Sort :
1 2 3 4 5
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program To Perform Quick Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
int dim, a[10];
void qsort(int, int);
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int i, l, r;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
l=0;
r=dim-1;
qsort(l, r);
cout<<" \n After Quick Sort : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* Source Code For Computing Quick Sort */
void qsort(int left, int right)
{
int i, j, p, temp;
if(right>left)
{
i=left;
j=right;
p=a[left];
while(right>left)
{
do
{
i++;
}
while( (a[i]<=p) && (i<=right) );
while( (a[j]>=p) && (j>left) )
{
--j;
}
if(j>i)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[left];
a[left]=a[j];
a[j]=temp;
qsort(left, j-1);
qsort(i, right);
}
}
/* End Of Main Program */
Output :
Enter Dimension : 5
Enter Values For Array :
3 1 5 2 4
After Quick Sort :
1 2 3 4 5
No comments:
Post a Comment