我认为您的解决方案过于复杂。您可以使用输入中接收到的单个数组来实现基数,并且每个步骤中的存储桶都由一个索引数组来表示,这些索引标记了输入数组中每个存储桶的起始索引。
实际上,您甚至可以递归地执行此操作:
// Sort 'size' number of integers starting at 'input' according to the 'digit'th digit// For the parameter 'digit', 0 denotes the least significant digit and increases as significance doesvoid radixSort(int* input, int size, int digit){ if (size == 0) return; int[10] buckets; // assuming decimal numbers // Sort the array in place while keeping track of bucket starting indices. // If bucket[i] is meant to be empty (no numbers with i at the specified digit), // then let bucket[i+1] = bucket[i] for (int i = 0; i < 10; ++i) { radixSort(input + buckets[i], buckets[i+1] - buckets[i], digit+1); }}当然
buckets[i+1] - buckets[i]在
i9 时会导致缓冲区溢出,但是我省去了额外的检查或可读性;我相信您知道如何处理。
这样,您只需要调用就可以对
radixSort(testcases, sizeof(testcases) / sizeof(testcases[0]),0)数组进行排序。



