Created
March 13, 2015 22:55
-
-
Save frydaykg/205ac5cf73bbb753dc21 to your computer and use it in GitHub Desktop.
quick_sort.cc
This file contains hidden or 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
| #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