#include #include #include #include "book.h" #include "compare.h" int POW2[9] = {1, 2, 4, 8, 16, 32, 64, 128, 256}; #define pow2(X) POW2[X] template void radix(Elem A[], Elem B[], int n, int k, int r, int cnt[]) { // cnt[i] stores number of records in bin[i] int j; for (int i=0, rtok=1; i=0; j--) B[--cnt[(A[j]/rtok)%r]] = A[j]; for (j=0; j void sort(Elem* array, int n) { static Elem* temp = NULL; static int* cnt = NULL; if (THRESHOLD == 0) { cout << "Need to set THRESHOLD\n"; exit(0); } if (temp == NULL) temp = new Elem[n]; // Declare temp array if (cnt == NULL) cnt = new int[pow2(THRESHOLD)]; // Declare temp array radix(array, temp, n, (sizeof(Elem) * 8)/THRESHOLD, pow2(THRESHOLD), cnt); } #include "sortmain.cpp"