Merge-sort a string

Given a list of n n strings each of length n n . What will be the worst case running time of sorting the strings in lexicographic order using merge-sort algorithm?

O ( n 2 log n ) O(n^{2}\log n) O ( n 2 ) O(n^{2}) O ( n log n ) O(n\log n) O ( 2 n log n ) O(2^{n}\log n)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Christopher Boo
May 15, 2016

Merge Sort needs O ( n lg n ) O(n\lg n) comparisons to sort n n objects. To compare two strings of length n n in lexicographic order, we compare the characters one-by-one until one where both are different. The worst case of this is O ( n ) O(n) where every characters are the same. Hence, O ( n lg n ) O(n\lg n) comparisons and each comparisons need O ( n ) O(n) , the answer is O ( n 2 lg n ) O(n^2\lg n) .

But we can simply compare two strings using relational operators then, for comparison it would cost O(1).

Georgi Lyubenov
Mar 7, 2016

Why level 3? Lexicographically = letter by letter, so if every string is of length n then at worst you would have to compare n times.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...