Good day. I wrote the quick sorting function of the array in descending order in c ++, but for some reason it sorts the array not completely, the problem is probably in the algorithm, but I could not understand what was wrong. Here is the code:
void quick_sort(int* a, int n){ if(n <= 1) return; // если массив из одного элемента или меньше, то не сортируем int c = n/2; int l = 0; int r = n-1; int piv = a[c]; while(l < r){ while(a[l] > piv) l++; while(a[r] < piv) r--; if(l < r){ int tmp = a[l]; a[l++] = a[r]; a[r--] = tmp; } } quick_sort(a, c); quick_sort(a+c, nc); } Here is an example of his work:
massiv: 8 8 1 10 2 9 2 6 2 9 quick sort: 10 9 9 2 1 8 8 6 2 2