快排的實現(分治)
阿新 • • 發佈:2019-01-07
#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);
}
}