简体   繁体   中英

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. 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 }

And after one more character each is revealed, the set is { form, fore, star, stop, stoc }

The new sorted set should be {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.

If you want to do this in O(n) you have to somehow keep track of "groups":

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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