Here is a terrible, untested combination of c and C ++ that shows one way to handle strings. There are many ways to improve it, both in clarity and in performance ...
The first thing to solve would be some way to avoid creating a large number of vectors on the stack. @Comingstorm's idea of ββusing two arrays is a good place to start.
const int numblocks = 256; void radixSort(String rsarr[],int length, int offset = 0) { int inplace = 0; vector<String> blocks[numblocks]; //split the strings into bins for (int i=0;i<length;i++) { char c = rsarr[i][offset]; if (c!='\0') blocks[(int)c].push_back(rsarr[i]); else //put the null strings up front rsarr[inplace++]=rsarr[i]; } //for blocks all except the null terminated one, // copy back into original array in order, // then radix sort that portion of the array for (int b=1;b<256;b++) { for (int j=0;j<blocks[b].length();j++) rsarr[inplace++]=blocks[b][j]; if (j>1) radixSort(rsarr[inplace-j],j,offset+1); } }
source share