Quick Sort
Anonymous
c_cpp
02/19/2022 4:24 AM
752 B
20
Indexable
int partition(std::vector<int>& arr, size_t start, size_t end)
{
int x = arr[start];
int i = start;
for(size_t j = start + 1; j < end; j++)
{
if(arr[j]<=x)
{
i=i+1;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i], arr[start]);
return i;
}
void quick_sort(std::vector<int>& arr, size_t start, size_t end)
{
if(start < end)
{
int pivot = partition(arr, start, end);
quick_sort(arr, start, pivot);
quick_sort(arr, pivot + 1, end);
}
}
Editor is loading...