Quick Sort in non-increasing order. - C++
Wed Feb 26 2025 01:38:24 GMT+0000 (Coordinated Universal Time)
Saved by
@Rohan@99
#include <iostream>
using namespace std;
int Partition(int a[], int low, int high)
{
int pivot = a[low];
int i = low;
int j = high;
while(i < j)
{
while(a[i] >= pivot && i <= high - 1)
++i;
while(a[j] < pivot && j >= low + 1)
--j;
if(i < j)
swap(a[i], a[j]);
}
swap(a[low], a[j]);
return j;
}
void QuickSort(int a[], int low, int high)
{
if(low < high)
{
int pivotIndex = Partition(a, low, high);
QuickSort(a, low, pivotIndex - 1);
QuickSort(a, pivotIndex + 1, high);
}
}
int main()
{
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; ++i)
cin >> a[i];
QuickSort(a, 0, n-1);
for(int i = 0; i < n; ++i)
cout << a[i] << " ";
return 0;
}
content_copyCOPY
Comments