Skip to content

Instantly share code, notes, and snippets.

@h0hmj
Last active March 19, 2018 10:04
Show Gist options
  • Save h0hmj/7f1008d83587a13c72ac029e9be44094 to your computer and use it in GitHub Desktop.
Save h0hmj/7f1008d83587a13c72ac029e9be44094 to your computer and use it in GitHub Desktop.
quick sort
#include <utility>
#include <vector>
void quick_sort(std::vector<int>& nums, int l, int r) {
if (l >= r) {
return;
}
// pivot is left
int tmp = l;
for (int i = l + 1; i <= r; i++) {
if (nums[i] < nums[l]) {
std::swap(nums[++tmp], nums[i]);
}
}
std::swap(nums[tmp], nums[l]);
quick_sort(nums, l, tmp - 1);
quick_sort(nums, tmp + 1, r);
}
void quick_sort(std::vector<int>& nums) {
quick_sort(nums, 0, nums.size()-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment