Skip to content

Instantly share code, notes, and snippets.

@frydaykg
Created March 13, 2015 22:55
Show Gist options
  • Select an option

  • Save frydaykg/205ac5cf73bbb753dc21 to your computer and use it in GitHub Desktop.

Select an option

Save frydaykg/205ac5cf73bbb753dc21 to your computer and use it in GitHub Desktop.
quick_sort.cc
#include "sort.h"
#include "utils.h"
int Partition(int *a, int l, int r);
void Sort(int *a, int l, int r) {
if (r - l > 1) {
int m = Partition(a, l, r);
Sort(a, l, m);
Sort(a, m + 1, r);
}
}
int Partition(int *a, int l, int r) {
r--;
int pivot = a[r];
int i = l - 1;
for (int j = l; j < r; j++)
if (a[j] <= pivot)
swap(a[++i], a[j]);
swap(a[++i], a[r]);
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment