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); } }
原文:http://www.cnblogs.com/m2492565210/p/7257921.html