步骤
void quick_sort(int q[], int l, int r) { if (l >= r) return; //递归结束条件 int i = l - 1, j = r + 1, x = q[l + r >> 1]; //定义i, j指针, 确定分界点x(一般取中间值) while (i < j) { do i ++; while (q[i] < x); do j --; while (q[j] > x); if (i < j) swap(q[i], q[j]); //交换两值 } quick_sort(q, l, j), quick_sort(q, j + 1, r); }
步骤
void merge_sort(int q[], int l, int r) { if (l >= r) return; int mid = l + r >> 1; //确定分界点 merge_sort(q, l, mid); merge_sort(q, mid + 1, r); int k = 0, i = l, j = mid + 1; while (i <= mid && j <= r) { if (q[i] < q[j]) temp[k ++] = q[i ++]; else temp[k ++] = q[j ++]; } while (i <= mid) temp[k ++] = q[i ++]; while (j <= r) temp[k ++] = q[j ++]; for (int i = l, j = 0; i <= r; i ++, j ++) q[i] = temp[j]; }