


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



//...........#include
Node* next; }; int countDigits(int x){ int count = 0; while (x != 0){ x = x / 10; count++; } return count; } void initializeBins(Node** p, int n){ for (int i=0; i<n; i++){ p[i] = nullptr; } } void Insert(Node** ptrBins, int value, int idx){ Node* temp = new Node; temp->value = value; temp->next = nullptr; if (ptrBins[idx] == nullptr){ ptrBins[idx] = temp; // ptrBins[idx] is head ptr } else { Node* p = ptrBins[idx]; while (p->next != nullptr){ p = p->next; } p->next = temp;
Insert(bins, A[i], binIdx); } // Update A with sorted elements from bin int i = 0; int j = 0; while (i < 10){ while (bins[i] != nullptr){ A[j++] = Delete(bins, i); } i++; } // Initialize bins with nullptr again initializeBins(bins, 10); } // Delete heap memory delete [] bins; } int main() { int A[] = {237, 146, 259, 348, 152, 163, 235, 48, 36, 62}; int n = sizeof(A)/sizeof(A[0]); Print(A, n, "\t\tA"); RadixSort(A, n); Print(A, n, " Sorted A"); return 0; }