I was wondering about the complexity of quicksort implemented in this post.
Stuart Marks says it's O(N^2 log N). But is it really? I don't understand these words:
It seems to me -- and once again, I'm not a C# or .NET expert -- that this will cause certain innocuous-looking calls, such as pivot selection via ints.First(), to be more expensive than they look. At the first level, of course, it's O(1). But consider a partition deep in the tree, at the right-hand edge. To compute the first element of this partition, the entire source has to be traversed, an O(N) operation. But since the partitions above are lazy, they must be recomputed, requiring O(lg N) comparisons. So selecting the pivot would be an O(N lg N) operation, which is as expensive as an entire sort.
Why would ints.First()
be an O(N) operation? I think it's always O(1). And why do the partitions above in the tree of IEnumerables
have to be recomputed? This also doesn't make any sense to me. Doesn't IEnumerable.Where
return a new IEnumerable? Seems to me like the time complexity of this algorithm is still O(N log N), but the space complexity is O(N log N) as well, instead of just O(N) that we have where we sort in-place.
All in all is Stuart Marks right or am I right?