1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
template<typename T, size_t size> T partition(T (&A)[size], int left, int right)
{
T p = median(A, left, right);
int i = left;
int j = right;
std::swap(A[j / 2], A[i]);
for (;;)
{
while (A[++i] < p) if (i >= right) break;
while (A[--j] > p) if (j <= left) break;
if (i >= j) break;
else std::swap(A[i], A[j]);
}
if (j == left) return j;
std::swap(A[left], A[j]);
return j;
}
template<typename T, size_t size> void quicksort(T (&A)[size], int left, int right)
{
int q;
if (right > left)
{
q = partition(A, left, right);
quicksort(A, q + 1, right);
quicksort(A, left, q - 1);
}
}
|