first i had partioned my array into two subarrays. Then the quick sort switches to insertion sort for sorting the small sized subarrays. But the error is logic error, because when i enter 15 inputs, the first and the last inputs are not sorted at all..I think error is in my loop.so please guide me to remove this.
#include<iostream>
using namespace std;
void quicksort(int a[],int p,int r);
int partition(int a[],int p,int r);
void quicksort(int a[],int p,int r)
{
if(p<r)
{
int q;
q=partition(a,p,r);
if((p-r)<(r+1))
{
int i=0;
int key;
for(int j=1;j<(r+1);j++)
{
key=a[j];
i=j-1;
while(i>0&&a[i]>key)
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=key;
}
}
else
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
}
int partition(int a[],int p,int r)
{
int x=a[r];
int i=p-1;
int j;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i++;
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int temp1;
temp1=a[i+1];
a[i+1]=a[r];
a[r]=temp1;
return i+1;
}
void main()
{
int a[15],p=0,r=14;
for(int b=0;b<=15;b++)
{
cin>>a[b];
}
quicksort(a,p,r);
for(int c=0;c<=15;c++)
{
cout<<" "<<a[c];
}
system("pause");
}