Skip to content

Instantly share code, notes, and snippets.

@unworthyEnzyme
Created November 24, 2022 15:48
Show Gist options
  • Save unworthyEnzyme/2f9b8ba62b3745134e0043f074f0bbf5 to your computer and use it in GitHub Desktop.
Save unworthyEnzyme/2f9b8ba62b3745134e0043f074f0bbf5 to your computer and use it in GitHub Desktop.
quicksort in c++
#include <vector>
auto partition(std::vector<int>& arr, int start, int end) -> int {
auto pivotIndex = end;
for (int i = start; i < pivotIndex; i++) {
if (arr[i] > arr[pivotIndex]) {
auto temp = arr[i];
arr[i] = arr[pivotIndex];
arr[pivotIndex] = temp;
pivotIndex = i;
}
}
return pivotIndex;
}
auto quicksort(std::vector<int>& arr, int start, int end) {
if (start >= end) return;
auto pivot = partition(arr, start, end);
quicksort(arr, start, pivot - 1);
quicksort(arr, pivot + 1, end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment