1. 程式人生 > >快排的實現(分治)

快排的實現(分治)

#define SWAP(x, y) {int t=x; x=y; y=t;}

void quickSort(int *a, int left, int right)
{
    if (left < right)
    {
        int t = a[(left+right)/2];
        int i = left - 1;
        int j = right + 1;

        while (1)
        {
            while (a[++i] < t);
            while (a[--j] > t);

            if
(i >= j) { break; } SWAP(a[i], a[j]); } quickSort(a, left, i-1); quickSort(a, j+1, right); } }

快排的理解