What will be the worst complexity for sorting n
strings having n
characters each? Will it be just n
times its avg. case O(n log n)
or something else...?
相关问题
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- how to split a list into a given number of sub-lis
- Generate string from integer with arbitrary base i
- Finding k smallest elements in a min heap - worst-
相关文章
- JSP String formatting Truncate
- What are the problems associated to Best First Sea
- Selecting only the first few characters in a strin
- Coin change DP solution to keep track of coins
- Sort TreeView Automatically Upon Adding Nodes
- Python: print in two columns
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
When you are talking about
O
notation with two things with different lengths, typically you want to use different variables, likeM
andN
.So, if your merge sort is
O(N log N)
, whereN
is the number of strings... and comparing two strings isO(M)
whereM
scales with the length of the string, then you'll be left with:or
where
M
is the string length andN
is the number of strings. You want to use different labels because they don't mean the same thing.In the strange case where the average string length scales with the number of strings, like if you had a matrix stored in strings or something like that, you could argue that
M = N
, and then you'd haveO(N^2 log N)
Sorting n items with MergeSort requires
O(N LogN)
comparisons. If the time to compare two items isO(1)
then the total running time will beO(N logN)
. However, comparing two strings of length N requiresO(N)
time, so a naive implementation might stuck withO(N*N logN)
time.This seems wasteful because we are not taking advantage of the fact that there are only
N
strings around to make comparisons. We might somehow preprocess the strings so that comparisons take less time on average.Here is an idea. Create a Trie structure and put N strings there. The trie will have
O(N*N)
nodes and requireO(N*N)
time to build. Traverse the tree and put an integer "ranking" to each node at the tree; If R(N1)<R(N2) then the string associated with Node1 comes before the string associated with Node2 in a dictionary.Now proceed with Mergesort, do the comparisons in
O(1)
time by looking up the Trie. The total running time will beO(N*N + N*logN)
=O(N*N)
Edit: My answer is very similar to that of @amit. However I proceed with mergesort where he proceeds with radixsort after the trie building step.
As @orangeoctopus, using standard ranking algorithm on a collection of
n
strings of sizen
will result inO(n^2 * logn)
computation.However - note that you can do it in
O(n^2)
, with variations on radix sort.The simplest way to do it [in my opinion] - is
O(n)
and you do itn
times - total ofO(n^2)
It is easy to see you cannot do it any better then
O(n^2)
, since only reading the data isO(n^2)
, thus this solution is optimal in terms of big O notation of time complexity.