I implemented quick sort using partitioning technique. The one issue I am facing is based on the pivot I need to change my code. Below is my implementation of qsort.
#include<iostream>
using namespace std;
void qsort1(int arr[], int p, int q)
{
if(p<q)
{
int ppos = p;
int pivot = arr[ppos];
int r = p;
for(int i=p;i<=q;i++)
{
if(arr[i] < pivot)
{
r++;
swap(arr[i],arr[r]);
}
}
swap(arr[r],arr[ppos]);
qsort1(arr,p,r-1);
qsort1(arr,r+1,q);
}
}
int main()
{
int arr[]= {9,7,4,1,2,3};
qsort1(arr,0,5);
for(int i =0;i<6;i++)
cout<<arr[i]<<endl;
return 0;
}
To change the pivot from first to last element I need to change my r to exclude the last element. can someone please suggest me a better implementation using same partitioning technique . By the way its not a homework question.
std::sort...<iostream>, then this is not a C program. I've edited your tags.