Last active
July 1, 2020 05:05
-
-
Save LazyRen/877d36376ecc7d02f9336c899fa7b350 to your computer and use it in GitHub Desktop.
Quick Sort Algorithm
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
template <typename T> | |
void swap(T& a, T& b) { | |
T tmp = a; | |
a = b; | |
b = tmp; | |
} | |
/* | |
* Sort all elements in arr[]; range of closed interval [left, right]. | |
*/ | |
template <typename T> | |
void quickSort(T arr[], int left, int right) { | |
if(left >= right) | |
return; | |
T pivot = arr[(left+right) / 2]; | |
int l = left, r = right; | |
while (l <= r) { | |
while (arr[l] < pivot) | |
l++; | |
while (arr[r] > pivot) | |
r--; | |
if (l <= r) | |
swap(arr[l++],arr[r--]); | |
} | |
quickSort(arr, left, r); | |
quickSort(arr, l, right); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment