voidquick_sort(int a[], int l ,int r) { if (l >= r) return; int mid = a[(l + r) / 2], i = l - 1, j = r + 1; // i - 1 和 j + 1 都是为了处理边界问题 while (i < j) { do i ++; while (a[i] < mid); do j --; while (a[j] > mid); // 不需要判定相等的情况 if (i < j) swap(a[i], a[j]); } quick_sort (a, l, j); quick_sort (a, j + 1, r); }
intmain() { scanf("%d", &n); for (int i = 0; i < n; i ++) scanf("%d", &a[i]); quick_sort (a, 0, n - 1); for (int i = 0 ;i < n; i ++) printf("%d ", a[i]); return0; }
constint N = 1000000; int n; int q[N + 10], ans[N + 10];
voidmerge_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]) ans[k ++] = q[i ++]; else ans[k ++] = q[j ++]; while (i <= mid) ans[k ++] = q[i ++]; while (j <= r) ans[k ++] = q[j ++]; for (int i = l, j = 0; i <= r; i ++, j ++) q[i] = ans[j]; } intmain() { scanf("%d", &n); for (int i = 0; i < n; i ++) scanf("%d" ,&q[i]); merge_sort(q, 0, n - 1); for (int i = 0; i < n; i ++) printf("%d", q[i]); return0; }