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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <stdio.h> #include <stdlib.h> #define MAX 10000 #define DEBUG 1 void output(int, int, int);
int arr[MAX], n; void start() { printf("请输入元素数量:"); scanf("%d", &n); printf("请输入 %d 个整数:\n", n); for (int i = 0; i < n; i++) { scanf("%d", arr + i); } while (getchar() != '\n'); return; }
void sort(int* arr, int l, int r) { if (l >= r) return; int x = l, y = r, z = arr[l]; while (x < y) { while (x < y && arr[y] >= z) --y; if (x < y) arr[x++] = arr[y]; while (x < y && arr[x] <= z) ++x; if (x < y) arr[y--] = arr[x]; } arr[x] = z; output(l, x, r); sort(arr, l, x - 1); sort(arr, x + 1, r); return; }
void output(int l, int x, int r) { if (!DEBUG) return; printf("\n待排序区间范围 [%d, %d]\n", l, r); printf("基准值:%d\n", arr[x]);
char str[30]; int cnt = 2; for (int i = 0; i < x; i++) { cnt += sprintf(str, "%d ", arr[i]); } for (int i = 0; i < l; i++) printf("%d ", arr[i]); printf("[ "); for (int i = l; i <= r; i++) { printf("%d ", arr[i]); } printf("] "); for (int i = r + 1; i < n; i++) printf("%d ", arr[i]); printf("\n"); for (int i = 0; i < cnt; i++) printf(" "); printf("^\n"); printf("按回车继续..."); while (getchar() != '\n'); return; }
int main() { start(); sort(arr, 0, n - 1); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
|