4

Assume we have an string array str = ["foo", "bar", "zebras", "topper"] which I need to to sort to get ["bar", "foo", "zebra", "top"], the sorting complexity will be O(nlogn) where n is the length of the array. But we also do string compare, which should be O(m) where m is the length of longest strings (such as zebras and topper). So final complexity should be O(m * nlogn). Please correct me if I am wrong.

This question differs from here because, here I am comparing all strings with all strings, rather than a fixed string with just one.

0

1 Answer 1

2

This is the result you would get if you performed sort using only normal string comparison.

You can get better performance (but I don't now of any implementations of this) by knowing your inputs are strings. For example, if you sort the strings by each character in turn, which gives O(nm|alphabet|).

Sign up to request clarification or add additional context in comments.

2 Comments

So assuming I only used normal String comparison, am I correct. ?
Yes, you are. Usually the complexity of sort is expressed in terms of number of comparisons (and swaps), so if your comparisons are O(m) (as they are), the sort is O(mnlog(n))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.