I’ve made this program as the demonstration of Quick Sort.
I’ve defined it for 5 inputs but it is taking infinite.
please tell me the Mistake in it.
#include<iostream>
using namespace std;
int a[5];
void QSort(int,int);
int Divide(int,int);
int main()
{
int i;
cout<<"Enter Elements : "<<endl;
for(i=0;i<5;i++)
{cin>>a[i];}
QSort(0,4);
cout<<"Sorted Elements are : "<<endl;
for(i=0;i<5;i++)
cout<<a[i];
return 0;
}
void QSort(int first,int last)
{
if(first<last)
{
int index=Divide(first,last);
QSort(first,index-1);
QSort(index+1,last);
}
}
int Divide(int f,int l)
{
int pivot=a[f];
int left=f+1;
int right=l;
while(true)
{
while((a<=pivot)&&(left<right))
left++;
while((a[right]>=pivot)&&(right>left))
right--;
if(left<right)
{
int temp=a;
a=a[right];
a[right]=temp;
}
}
int temp=a;
a=a[f];
a[f]=temp;
return left;
}