Skip to content

Instantly share code, notes, and snippets.

@LazyRen
Last active July 1, 2020 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LazyRen/877d36376ecc7d02f9336c899fa7b350 to your computer and use it in GitHub Desktop.
Save LazyRen/877d36376ecc7d02f9336c899fa7b350 to your computer and use it in GitHub Desktop.
Quick Sort Algorithm
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