Reputation: 49
I have been studying for my Algorithms midterm and there is this question in the book that I came across about sorting variable length items :
I found many answers online but they weren't very clear in their explanations so I would really appreciate it if you could take the time to explain to me more in depth what this answer suggests should be done to sort the strings in O(n) time:
Upvotes: 3
Views: 2000
Reputation: 1563
This is what you are searching for:https://en.wikipedia.org/wiki/Radix_sort
In simple words:
You start sorting by the first digit of the strings. This can be done in one pass O(N), since you do not need to compare each element with others. You just need to remember the starting and ending position of each group of values in the array. For example all strings starting with 'g' are located in array positions 35 to 500. When you find a string stating with 'g' you add it to the end of this group.
In the next phase you do the same for each of these groups.
As you can see, it takes O(M*N) where M is the string length and N is the number of strings. In your case your N is total length of all strings so it's O(N).
Although you have strings in different lengths you can still keep the O(N), because at some point items that are too short will not need to be re-sorted.
Upvotes: 2