Created
November 24, 2022 15:48
-
-
Save unworthyEnzyme/2f9b8ba62b3745134e0043f074f0bbf5 to your computer and use it in GitHub Desktop.
quicksort in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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