[Algorithm] Sorting Time Complexity of String Arrays

Question: There is an array of strings, and each string in the array is sorted alphabetically; then the entire array of strings is sorted lexicographically. Time complexity of the whole operation?

analyze:

  • 1. Assume the longest string length is s; there are n strings in the array

  • 2. Sort each string: O(slogs)

  • 3. Sort each string in the array alphabetically: O(n*slogs)

  • 4. Sort the entire string array lexicographically: O(s*nlogn)

comprehensive:

Overall time complexity O = O(n slogs) + O(s nlogn) = O(n slogs+s nlogn) = O(n s (logn+logs))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325264826&siteId=291194637