#include <time.h> #include <stdio.h> #include <malloc.h> #include <stdlib.h> int *array; int cnt, head, tail; void random(int cnt, int head, int tail) { srand((unsigned)time(NULL)); for(int i = 0; i < cnt; i++) array[i] = head + rand()%(tail-head+1); } void init() { printf("生成随机数个数: "); scanf("%d", &cnt); array = (int *)malloc(cnt * sizeof(int)); printf(" 随机数上下限设置为(如1,100): "); scanf("%d%*c%d", &head, &tail); random(cnt, head, tail); } void showArray() { for(int i = 0; i < cnt; i++) { printf("%8d", array[i]); printf(((i+1)%8==0) ? " " : " "); } } /*********************** COMMON FUNCTION ***********************/ int lgr(int a, int b) { return (a>b) ? 1 : 0; } void merge(int *c, int *d, int l, int m, int r) /* 实现各具体片段的归并操作 */ { int i = l, j = m+1, k = l; while(i<=m && j<=r) { if(lgr(c[i], c[j])) d[k++] = c[j++]; else d[k++] = c[i++]; } if(i > m) for(int q = j; q <= r; q++) d[k++] = c[q]; else for(int q = i; q <= m; q++) d[k++] = c[q]; } void mergePass(int *x, int *y, int s) /* 对给定规模的归并作宏观讨论 */ { int i = 0; while(cnt-i >= 2*s) // ? < 2*s { merge(x, y, i, i+s-1, i+2*s-1); i = i+2*s; } if(cnt-i > s) merge(x, y, i, i+s-1, cnt-1); // s < ? < 2*s else for(int j = i; j < cnt; j++) y[j] = x[j]; // ? <= s } void mergeSort(int *a) /* 控制整个数列归并规模的变化过程 */ { int *b = (int *)malloc(cnt * sizeof(int)); int s = 1; // s = 1, 2, 4, 8... while(s < cnt) { mergePass(a, b, s); s *= 2; mergePass(b, a, s); s *= 2; } } /************************* MERGE SORT *************************/ int cmp(int a, int b) { return (a>b) ? 1 : ((a==b)?0:-1); } void swap(int i, int j) { int temp; temp = array[i]; array[i] = array[j]; array[j] = temp; } int partion(int p, int r) { int i = p, j = r+1; int x = array[p]; while(true) { while(cmp(array[++i], x) < 0 && i < r); // 防止i越过右界 while(cmp(array[--j], x) > 0); // j至多走到p处 if(i >= j) break; swap(i, j); } array[p] = array[j]; array[j] = x; return j; } void quickSort(int p, int r) /* 快排采用分治思想 */ { if(p < r) { int q = partion(p, r); quickSort(p, q-1); quickSort(q+1, r); } } /************************* QUICK SORT *************************/ int main() { init(); printf(" 生成随机数列如下: "); showArray(); mergeSort(array); //quickSort(0, cnt-1); printf(" 随机数列排序后如下: "); showArray(); free(array); return 0; }
非递归快速排序:
#include <stack> #include <time.h> #include <stdio.h> #include <malloc.h> #include <stdlib.h> using namespace std; int *array; int cnt, head, tail; class Range // 定义Range类放入栈中 { public: int head; int tail; Range(){}; Range(int p, int r):head(p),tail(r){} }; void random(int cnt, int head, int tail) { srand((unsigned)time(NULL)); for(int i = 0; i < cnt; i++) array[i] = head + rand()%(tail-head+1); } void init() { printf("生成随机数个数: "); scanf("%d", &cnt); array = (int *)malloc(cnt * sizeof(int)); printf(" 随机数上下限设置为(如1 100): "); scanf("%d%*c%d", &head, &tail); random(cnt, head, tail); } void showArray() { for(int i = 0; i < cnt; i++) { printf("%8d", array[i]); printf(((i+1)%8==0) ? " " : " "); } } int cmp(int a, int b) { return (a>b) ? 1 : ((a==b)?0:-1); } void swap(int i, int j) { int temp; temp = array[i]; array[i] = array[j]; array[j] = temp; } int partion(int p, int r) { int i = p, j = r+1; int x = array[p]; while(true) { while(cmp(array[++i], x) < 0 && i < r); // 防止i越过右界 while(cmp(array[--j], x) > 0); // j至多走到p处 if(i >= j) break; swap(i, j); } array[p] = array[j]; array[j] = x; return j; } void quickSort(int p, int r) { stack<Range> ss; ss.push(Range(p, r)); int q; // q为归位元素下标 Range node; while(!ss.empty()) { node = ss.top(); ss.pop(); q = partion(node.head, node.tail); if(node.head < q-1) ss.push(Range(node.head, q-1)); if(q+1 < node.tail) ss.push(Range(q+1, node.tail)); } } int main() { init(); printf(" 生成随机数列如下: "); showArray(); quickSort(0, cnt-1); printf(" 随机数列排序后如下: "); showArray(); free(array); return 0; }