栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用C ++实现的Radix Sort

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用C ++实现的Radix Sort

我认为您的解决方案过于复杂。您可以使用输入中接收到的单个数组来实现基数,并且每个步骤中的存储桶都由一个索引数组来表示,这些索引标记了输入数组中每个存储桶的起始索引。

实际上,您甚至可以递归地执行此操作:

// 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]
i
9 时会导致缓冲区溢出,但是我省去了额外的检查或可读性;我相信您知道如何处理。

这样,您只需要调用就可以对

radixSort(testcases, sizeof(testcases) / sizeof(testcases[0]),0)
数组进行排序。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609339.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号