简体   繁体   中英

Does chaining sort and reduce increase time complexity from O(n) to O(n ^ 2)?

In terms of time complexity, is this:

let string = "I am a little teapot"
let wordArray = string.split(' ').sort()
let store = wordArray.reduce( (acc, word) => {
       acc[word] = acc[word] + 1 || 1;
       return acc;
     }, {}) 

any better than this? :

let string = "I am a little teapot"
let store = string.split(' ').sort().reduce( (acc, word) => {
      acc[word] = acc[word] + 1 || 1;
      return acc;
    }, {}) 

I believe that each of these has time complexity O ( n ). Am I right?

Lots of different things:

  1. Chaining doesn't impact time complexity in any way in this case. It is just a syntaxical difference.

  2. I assume that n designs the number of words in the sentence you want to process. O(n) is an incorrect estimation for the time complexity. An efficient comparison sort usually takes O(nlog(n)) comparisons. Besides here, you are comparing strings, so the comparison is not made in constant time (with respect to the length of the strings). This doesn't change the fact that the complexity is O(nlog(n)) though since the length of the strings probably doesn't depend on n.

  3. O(3n) = O(2n) = O(n) by definition, that's precisely what O means. Check out your Landau notation .

Edit: following a commentary let me make the reasoning on the complexity clearer.

The sort function is called on the result of splitting a sentence on whitespaces, which comes to down to applying sort to a sequence of words.

I don't know how sort is implemented but it is very likely a comparison sort, meaning that it roughly does O(n*log(n)) comparisons where n is the number of items being sorted: here the words of the sentence.

Now, we know that we will do O(n*log(n)) string comparisons, which is not in constant time with respect to the length of the strings. String comparison takes at worst L operations, with L the length the minimum length of the two strings being compared. Let's now take L as the maximum length of the strings being sorted. From what has been said, we will do roughly O(L*n*log(n)) operations to sort the array. Since there is no reason for L to depend on n, we have that O(L*n*log(n)) = O(n*log(n)) operations for the time compexity.

Does chaining increase time complexity?

No, intermediate variables do not change the time complexity, the algorithm is exactly the same.

I believe that each of these has time complexity O(n) . Am I right?

No, although you haven't stated what n measures.

The complexity of your algorithm can be expressed as O(s + (s/w) w log w) , where s is the length of string , w the number of words in it and s/w the average word length (time needed for the string comparison); which simplifies to O(s log w) .

No, this doesn't make any difference (actually, maybe a very very very tiny, but very far to up the complexity from O(n) to O(n^2)).

And you're correct, your code is O(n log n), which n is the number of words from your string.

The sort method probably is implemented using quicksort, which is O(n log n) in the average case, and your reduce is a simple loop, O(n).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM