1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

int partition(int a[], int l, int r)
{
    int i = l;
    int j = r + 1;
    int v = a[l];
    
    while (true) {
        while (a[--j] > v);
        while (i < j && a[++i] < v);
        
        if (i == j)
        {
            break;
        }
        std::swap(a[i], a[j]);
    }
    
    std::swap(a[i], a[l]);
    
    return i;
}

void quick_sort(int a[], int l, int r)
{
    if (l < r) {
        int m = partition(a, l, r);
        quick_sort(a, l, m - 1);
        quick_sort(a, m + 1, r);
    }
}

int main(int argc, const char * argv[])
{
    int a[] = {1, 1, 6, 6, 8, 3, 5, 100, 300, 200, 99, 99};
    quick_sort(a, 0, sizeof(a)/sizeof(a[0]) - 1);
    
    for (int i : a) {
        std::cout << i << ' ';
    }
    std::cout << std::endl;
    
    return 0;
}