简体   繁体   中英

Big O notation with Linq

Considering the following linq methods are the following:

// distinct() is O(n)
// toList is O(n)
// OrderBy is O(n log(n))

And I were to do this operation on a string:

string s = "efgabcddddddaaaaaaaaaaa";
List<char> myList = s.Distinct().OrderBy(q => q).ToList();

Is chaining the Distinct().OrderBy().ToList() resulting in this function being O(n^2 log(n)) or O(n + n log(n)) ? And if I were to add another linq method that's O(n), what would it result in?

With Big O Notation, one only needs to concern oneself with the dominant term, which is O(n log(n)). Adding other O(n) methods would therefore have no effect on the overall complexity of the algorithm.

See also https://softwareengineering.stackexchange.com/a/258511/119367

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