#include using namespace std; #include "apmatrix.h" #include "apqueue.h" int GetDigit(int number, int k) { for(int j=0; j < k; j++) { number = number/10; } return number % 10; } apvector > ItemsToQueues(const apvector& L, int k) { apvector > result(10); for(int j=0; j < L.length(); j++) { result[GetDigit(L[j],k)].enqueue(L[j]); } return result; } apvector QueuesToArray(apvector > & QA, int numVals) { apvector result(numVals); int index = 0; for(int j=0; j < 10; j++) { while (! QA[j].isEmpty()) { QA[j].dequeue(result[index]); index++; } } return result; } void RadixSort(apvector& L, int numDigits) { for(int j=0; j < numDigits; j++) { apvector > result = ItemsToQueues(L,j); L = QueuesToArray(result, L.length()); // line below isn't valid C++ though ok with Visual C++ // it fails with codewarrior and with g++ // L = QueuesToArray(ItemsToQueues(L,j), L.length()); } } int main() { apvector v(6); v[0] = 232; v[1] = 197; v[2] = 37; v[3] = 1028; v[4] = 338; v[5] = 106; RadixSort(v,4); int k; for(k=0; k < 6; k++) { cout << v[k] << " "; } cout << endl; }