This is my partition logic for quick sort, and it is providing me Garbage array element values as error. I think I am mistaken with my partition code but also i am not sure about that.
#include <bits/stdc++.h>
using namespace std;
void swap(int* x, int* y)
{
int temp = *y;
*y = *x;
*x = temp;
}
int partition(int* A, int left, int right)
{
int pivot = A[left];
int i = left + 1, j = right;
while (i <= j) {
if (A[i] <= pivot)
++i;
if (A[j] > pivot)
--j;
if (A[i] > pivot && A[j] <= pivot) {
swap(&A[i], &A[j]);
++i;
--j;
}
}
swap(&A[left], &A[j]);
return j;
}
void quickSort(int* A, int left, int right)
{
if (left >= right)
return;
int pi = partition(A, left, right);
quickSort(A, left, pi - 1);
quickSort(A, pi + 1, right);
}
int main()
{
int A[] = { 5, 8, 1, 31, 2, 44, 6, 0, 8, 4 };
int n = sizeof(A) / sizeof(A[0]);
quickSort(A, 0, n - 1);
for (auto i : A)
cout << i << " ";
return 0;
}