[英]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.