排序

来源:互联网 发布:ubuntu 16.04合理分区 编辑:程序博客网 时间:2024/06/07 05:02

一、快排

int getRandom(int start, int end) {    srand((int)time(0));    int idx = random() % (end - start + 1) + start;    return idx;}int partition(int *a, int len, int start, int end) {    int idx = getRandom(start, end);    if (idx != end) {        swap(a[idx], a[end]);    }        int small = start - 1;    for (idx = start; idx < end; ++ idx) {        if (a[idx] < a[end]) {            ++ small;            if (small != idx) {                swap(a[idx], a[small]);            }        }    }        ++ small;    swap(a[small], a[end]);        return small;}void quitSort(int *a, int len, int start, int end) {    if (start == end) {        return;    }    int idx = partition(a, len, start, end);    if (idx > start) {        quitSort(a, len, start, idx - 1);    }    if (idx < end) {        quitSort(a, len, idx + 1, end);    }}void myQuitSort(int *a, int len) {    quitSort(a, len, 0, len - 1);}


0 0
原创粉丝点击