Why quick sort in cpp is not working for :
//i have tried multiple code but i am keeep geeting error
number of 1 can be different but Quick sort is unable to solve it mainly in c++
working fine in python
{2,1,1,1,1,1,1,1,1,1,2}
#include <bits/stdc++.h>
using namespace std;
void swap(vector<int>& v, int x, int y);
void quicksort(vector<int> &vec, int L, int R) {
int i, j, mid, piv;
i = L;
j = R;
mid = L + (R - L) / 2;
piv = vec[mid];
while (i<R || j>L) {
while (vec[i] < piv)
i++;
while (vec[j] > piv)
j--;
if (i <= j) {
swap(vec, i, j); //error=swap function doesnt take 3 arguments
i++;
j--;
}
else {
if (i < R)
quicksort(vec, i, R);
if (j > L)
quicksort(vec, L, j);
return;
}
}
}
void swap(vector<int>& v, int x, int y) {
int temp = v[x];
v[x] = v[y];
v[y] = temp;
}
int main() {
vector<int> vec1 = {2,1,1,1,1,1,1,1,1,1,1,2};
const int count = 10;
// for (int i = 0; i < count; i++) {
// vec1.push_back(1 + rand() % 100);
// }
quicksort(vec1, 0, count - 1);
for (int i : vec1){
cout<<i <<" ";
}
}
output of the code
New contributor
TIME CREATORS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.