简体   繁体   English

如何在线性时间内对长度为k的n个单词进行排序?

[英]How can I sort n words with length k in linear time?

I want to implement an algorithm to sort n words of length k, the words will contain only English words (so from az). 我想实现一种算法来对长度为k的n个单词进行排序,这些单词将仅包含英语单词(因此来自az)。

I tried to use the counting sort by casting the first element of the word -> character to its integer representation and then use Counting Sort (Linear Time Sorting), this kind of works but it is only sorting the first character ie when two or more words have the same first character then they are not sorted by their second character? 我试图通过将单词->字符的第一个元素转换为其整数表示来使用计数排序,然后使用计数排序(线性时间排序),这种工作方式只是对第一个字符进行排序,即当两个或多个时单词具有相同的第一个字符,然后不按第二个字符排序?

Can someone guide me or give me a hint for another approach which will let me sort these n words in linear time? 有人可以指导我或给我提示另一种方法的方法,该方法可以让我在线性时间内对这n个单词进行排序?

Radix sort is probably the way to go - it will stable sort each word by the i-th letter. 基数排序可能是要走的路-它会按第i个字母稳定地对每个单词进行排序。 Since you're working with characters, you're essentially already working in the appropriate radix! 由于您正在使用字符,因此实际上您已经在使用适当的基数了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在O(n * log K)时间内对平均长度为K的排序列表进行排序? - how would you sort n sorted lists with average length K in O(n*log K) time? 有多少个长度为 n 的单词最多有 k 个连续的元音? - How many words of length n have at most k consecutive vowels? Bucket Sort O(n+k) 如果使用插入排序对每个桶进行排序,时间复杂度如何? - How is the time complexity of Bucket Sort O(n+k) if it uses insertion sort to sort each bucket? 长度为 N 的数组可以包含值 1,2,3 ... N^2。 是否可以在 O(n) 时间内排序? - An array of length N can contain values 1,2,3 ... N^2. Is it possible to sort in O(n) time? 如何获得基于O(n log k)-时间比较的算法对k-multiset进行排序? - How to get a O(n log k)-time comparison-based algorithm to sort k-multiset? 是否可以将O(k * n)视为线性复杂度(O(n))? - Can O(k * n) be considered as linear complexity (O(n))? 计数排序O(n + k)时间复杂度中的k是多少? - What is k in counting sort O(n+k) time complexity? 在线性时间内找到sum> = k的n个整数的最小子阵列 - Finding a minimal subarray of n integers of sum >= k in linear time 可以在线性时间内解决,在n ^ 2时间内完成 - can it be solved in linear time, did this in n^2 time 如何在不同的运行时间对给定记录进行排序 - O(n) 和 O(nlogk),其中 k 是具有键值对的序列 n 的不同值? - How can I sort a given record in different running times - O(n) and O(nlogk), where k is the distinct values for a sequence n with key-value pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM