Created
March 19, 2020 08:07
-
-
Save joonas-yoon/f4fad0c8b1e4aa19e3e99431e3731681 to your computer and use it in GitHub Desktop.
alternative for STL - sort(quick)
This file contains 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
// based on quick sort | |
template<typename T> | |
void sort(T *arr, T *end) { | |
int l = 0, r = (end - arr) - 1; | |
if (l >= r) return; | |
T mid = arr[(l + r) / 2]; | |
while (l <= r) { | |
while (arr[l] < mid) ++l; | |
while (mid < arr[r]) --r; | |
if (l <= r) { | |
swap(arr[l++], arr[r--]); | |
} | |
} | |
sort(arr, arr + l); | |
sort(arr + l, end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment