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
| #include <stdio.h> #include <stdlib.h> //冒泡排序 void Bubble_Sort(int num[], int n) { int i, j, k, z; int change = true; for (i = 0; i <= n - 1 && change; i++) { change = false; for (j = 0; j <= n - 1; j++) { if (num[j] > num[j + 1]) { k = num[j + 1]; num[j + 1] = num[j]; num[j] = k; change = true; } } printf("--\n第%d次遍历后的结果是:\n--", i + 1); for (z = 0; z <= n - 1; z++) { printf("%d\t", num[z]); } } } //快速排序 int QKPass(int num[], int low, int high) { int x = num[low]; int i, j; while (low < high) { //先找小于的数据 while (low < high && x <= num[high]) high--; if (low < high) { num[low] = num[high]; low++; } //找大于的数据 while (low < high && num[low] < x) low++; if (low < high) { num[high] = num[low]; high--; } } num[low] = x; return low; } void QKSort(int num[], int low, int high) { int pos; if (low < high) { pos = QKPass(num, low, high); QKPass(num, low, pos - 1); QKPass(num, pos + 1, high); } } int main() { int z; int num[10] = {0, 62, 35, 77, 55, 14, 35, 98, 22, 40}; int n = 10; // Bubble_Sort(num, n); QKSort(num, 0, 10); printf("\n"); for (z = 1; z <= n - 1; z++) { printf("%d -- %p \t", num[z]); } }
|