码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2017-07-30 00:19:22      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:center   quick   sort   swa   define   qsort   div   dia   type   

void Quicksort(ElementType A[], int N) {
    Qsort(A, 0, N-1);
}

ElementType Median3(ElementType A[], int Left, int Right) {
    int Center = (Left + Right)/2;
    if (A[Left] > A[Center])
        swap(&A[Left], &A[Center]);
    if (A[Left] > A[Right])
        swap(&A[Left], &A[Right]);
    if (A[Center] > A[Right])
        swap(&A[Center], &A[Right]);
    swap(&a[Center], &A[Right-1]);
    return a[Right-1];
}

#define Cutoff (3)
void Qsort(Element A[], int Left, int Right) {
    int i,j;
    ElementType Pivot;
    if (Left+Cutoff <= Right) {
        Pivot = Median3(A, Left, Right);
        i=Left;
        j=Right-1;
        for (;;) {
            while (A[++i] < Pivot);
            while (A[--j] < Pivot);
            if (i<j)
                swap(&A[i], &A[j]);
            else
                break;
        }
        swap(&A[i], &A[Right-1]);
        Qsort(A, Left, i-1);
        Qsort(A, i+1, Right);
    }
    else {
        insertionsort(A+Left, Right-Left+1);
    }
}

 

快速排序

标签:center   quick   sort   swa   define   qsort   div   dia   type   

原文地址:http://www.cnblogs.com/m2492565210/p/7257921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!