What's the asymptotic complexity of GroupBy operation?

后端 未结 3 1344
长发绾君心
长发绾君心 2020-12-19 07:26

I am interested in the asymptotic complexity (big O) of the GroupBy operation on unindexed datasets. What\'s the complexity of the best known algorithm and what\'s the compl

3条回答
  •  暖寄归人
    2020-12-19 07:52

    About Linq, I guess you want to know about the Linq-to-object group by complexity (Enumerable.GroupBy).

    Checking the implementation with ILSpy, it appears to me it is O(n). (.Net Framework 4 series.)

    It enumerates the source collection once. For each element, it computes its grouping key. Then it checks if it has already the key in a hashtable mapping to elements lists, adding the key to the hashtable if it is missing. Then it adds the element to the corresponding entry list in the hashtable.

提交回复
热议问题