typedef unsigned int word; #define MAXSIZE 200 word L1[MAXSIZE] = { 0 }; word L2[MAXSIZE] = { 0 }; // ================================================================== // 分治排序法,递归版 void merge(word *p,int left, int middle,int right) { int n1,n2; int i,j,k; n1 = middle - left + 1; n2 = right - middle; // 复制数据到临时数组 for(i = 0; i < n1; i++) L1[i] = *(p+left+i); L1[i] = ~0; // 哨兵,所有元素都不能达到的最大值 for(j = 0; j < n2; j++) L2[j] = *(p+middle+1+j); L2[j] = ~0; // 哨兵 // 排序 i = 0; j = 0; for(k = left; k <= right; k++) { if(L1[i] <= L2[j]) // 升序 { *(p+k) = L1[i]; i++; } else { *(p+k) = L2[j]; j++; } } } void merge_sort(word *p,int left, int right) { int middle; if(left < right) { middle = (left + right) / 2; merge_sort(p,left,middle); merge_sort(p,middle+1,right); merge(p,left,middle,right); } } // ================================================================== // 冒泡排序 // 逐个轮训交换,每次轮训出个最大值或者最小值放在队尾 void bubble_sort(word *p,int length) { word temp; int i,j; for(i = 0; i < length; i++) { for(j = i+1; j < length; j++) { //if(*(p+i) > *(p+j)) // 升序排列 if(*(p+i) < *(p+j)) { // 交换 temp = *(p+j); *(p+j) = *(p+i); *(p+i) = temp; } } } } // ================================================================== // 霍纳规则求多项式的值 // number:数组尾元素所在标号 unsigned long horner(word *p, word number,word x) { unsigned long result = 0; word count = number; while(count != 0) { result = *(p+count) + x * result; count--; } result += *p; return result; }