简体   繁体   English

左至右基数排序

[英]left to right radix sort

Radix sort sorts the numbers starting from lease significant digit to most significant digit. 基数对从有效数字到最高有效数字的数字进行排序。 I have the following scenario : 我有以下情况:

My alphabet is the english alphabet and therefore my "numbers" are english language strings. 我的字母是英语字母,因此我的“数字”是英语字符串。 The characters of these strings are revealed one at a time left to right. 这些字符串的字符一次从左到右显示。 That is, the most significant digit , for all strings, is revealed first and so on. 也就是说,所有字符串的最高有效数字都首先显示出来,依此类推。 At any stage, i will have a set of k character long strings, that is sorted. 在任何阶段,我都会有一组k个字符长的字符串进行排序。 At this point one character more is revealed for every string. 此时,每个字符串都显示一个字符。 And i want to sort the new set of strings. 我想对新的字符串集进行排序。 How do i do this efficiently without starting from scratch ? 我如何有效地做到这一点而无需从头开始?

For example if i had the following sorted set { for, for, sta, sto, sto } 例如,如果我有以下排序集{for,for,sta,sto,sto}

And after one more character each is revealed, the set is { form, fore, star, stop, stoc } 并且在每个字符都被揭示之后,集合是{形式,前部,星号,停止,stoc}

The new sorted set should be {fore, form, star, stoc, stop } 新的排序集应为{fore,form,star,stoc,stop}

I m hoping for a complexity O(n) after each new character is added, where n is the size of the set. 我希望添加每个新字符后的复杂度为O(n),其中n是集合的大小。

If you want to do this in O(n) you have to somehow keep track of "groups": 如果要在O(n)中执行此操作,则必须以某种方式跟踪“组”:

for, for | sta | sto, sto

Within this groups, you can sort the strings according to their last character keeping the set sorted. 在此组中,您可以根据字符串的最后一个字符对字符串进行排序,以保持对集合的排序。

Storing groups can be done in various ways. 可以通过多种方式存储组。 At first sight, I would recommend some kind of remembering offsets of group beginnings/endings. 乍一看,我会建议您记住组开始/结束的偏移量。 However, this consumes extra memory. 但是,这会占用额外的内存。

Another possibility might be storing the strings in some kind of prefix-tree , which correspond quite naturally to "adding one char after another", but I don't know if this is suitable for your application. 另一种可能是将字符串存储在某种前缀树中 ,这很自然地对应于“一个又一个字符的添加”,但是我不知道这是否适合您的应用程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM