I am writing a basic QuickSort method, but I am unable to find out the errors in the code. The program is returning the same input as after going through the sorting function.Although I cross-checked my code with other internet resources, I tried to find out the error myself, and there seems to be some error in the Partition function, but I'm not able to figure out what exactly is causing the error.
Below is my code.
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
void swap(int n1, int n2)
{
int temp = n1;
n1=n2;
n2=temp;
}
int Partition(vector<int> &A, int start, int end)
{
int pivot = A[end];
int Pindex = start;
for(int i=start;i<end;i++)
{
if(A[i]<=pivot)
{
swap(A[i],A[Pindex]);
Pindex++;
}
}
swap(A[Pindex],A[end]);
return Pindex;
}
void QuickSort(vector<int> &A, int start, int end)
{
if(start<end)
{
int Pindex = Partition(A,start,end);
// printf("Partition Index:%d \n",Pindex);
QuickSort(A,start,Pindex-1);
QuickSort(A,Pindex+1,end);
}
}
int main()
{
vector<int>A = {4,6,7,9,3,2,0,5};
cout<<"Before Sorting\n";
int length = A.size();
for(int i=0;i<length;i++)
{
printf("%d",A[i]);
}
printf("\n");
QuickSort(A,0,length-1);
cout<<"After Sorting:\n";
for(int i=0;i<length;i++)
{
printf("%d",A[i]);
}
return 0;
}