1.快速排序
/* By LYLtim */
void swap (int *a, int *b)
{
int t = *a; *a = *b; *b = t;
}
void QSort(int a[], int l, int r)
{
int pl = l, pr = r, key = a[(l+r)>>1];
while (pl < pr) {
while (a[pl] < key) pl++;
while (a[pr] > key) pr--;
if (pl <= pr) {
swap(&a[pl], &a[pr]);
pl++; pr--;
}
}
if (pl < r) QSort(a, pl, r);
if (pr > l) QSort(a, l, pr);
}
2.归并排序
/* By LYLtim */
void merge(int start, int mid, int end)
{
int pl, pr, p = start,
len1 = mid - start + 1,
len2 = end - mid,
left[len1], right[len2];
for (pl = 0; pl < len1; pl++) left[pl] = a[start+pl];
for (pr = 0; pr < len2; pr++) right[pr] = a[mid+1+pr];
pl = pr = 0;
while (pl < len1 && pr < len2)
if (left[pl] < right[pr]) a[p++] = left[pl++];
else a[p++] = right[pr++];
while (pl < len1) a[p++] = left[pl++];
while (pr < len2) a[p++] = right[pr++];
}
void sort(int start, int end)
{
if (start < end) {
int mid = (start + end) >> 1;
sort(start, mid);
sort(mid+1, end);
merge(start, mid, end);
}
}