Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.

Code:typedef unsigned long long UI64; typedef unsigned long long *PUI64; PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) { size_t mIndex[8][256] = {0}; /* index matrix */ PUI64 pDst, pSrc, pTmp; size_t i,j,m,n; UI64 u; for(i = 0; i < count; i++){ /* generate histograms */ u = pData[i]; for(j = 0; j < 8; j++){ mIndex[j][(size_t)(u & 0xff)]++; u >>= 8; } } for(j = 0; j < 8; j++){ /* convert to indices */ n = 0; for(i = 0; i < 256; i++){ m = mIndex[j][i]; mIndex[j][i] = n; n += m; } } pDst = pTemp; /* radix sort */ pSrc = pData; for(j = 0; j < 8; j++){ for(i = 0; i < count; i++){ u = pSrc[i]; m = (size_t)(u >> (j<<3)) & 0xff; pDst[mIndex[j][m]++] = u; } pTmp = pSrc; pSrc = pDst; pDst = pTmp; } return(pSrc); }