6

As in the title - what's the memory complexity of std::sort() and std::sort_heap()? (The latter requires std::make_heap() so I'd like to know its memory complexity as well.)

I've tried searching on these sites: http://www.cplusplus.com/reference/ http://en.cppreference.com/w/ but either I missed it or they only mention the time complexity. Is the memory complexity of said functions specified anywhere (in C++ standard or some other document)? Or maybe that is implementation-dependent?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
NPS
  • 6,003
  • 11
  • 53
  • 90
  • All three algorithms are in-place, i.e. additional memory is O(1). – user515430 Oct 09 '14 at 21:01
  • According to this: http://stackoverflow.com/questions/1840121/which-type-of-sorting-is-used-in-the-function-sort `std::sort()` might be using `introsort` which in turn uses `quicksort` which isn't in-place (stack for recursive calls). – NPS Oct 09 '14 at 21:17
  • Quicksort is very much in-place. Data is never moved to external data storage. Data is never put on the stack. Parameters about where you are in the container may be put on the stack. You can argue about whether memory is O(1), but not about being in-place. – user515430 Oct 09 '14 at 22:23
  • `intro-sort` is not. – gsamaras Oct 09 '14 at 22:24
  • 1
    The standard imposes no requirement on the memory cost. A hypothetical conforming implementation could allocate `O(2^N)` memory just for the lulz, though no sane library writer would do so. – T.C. Oct 10 '14 at 00:54
  • @user515430 - if they are in-place, doesn't that make them ***O(n)***? – jww Oct 12 '14 at 06:02
  • @NPS - I believe the memory complexity of both are ***O(n)***. SGI offers time complexity in their descriptions, but not space complexity. I think there's an implicit understanding that memory is not allocated. See [sort](http://www.sgi.com/tech/stl/sort.html) and [sort_heap](http://www.sgi.com/tech/stl/sort_heap.html) on the SGI pages. If stack space is needed, its probably on the order of ***O(log(n))***. That is, its a ***c * log(n)***. – jww Oct 12 '14 at 06:06
  • @jww The memory complexity is measured in additional memory needed. Hence if only fixed amount of additional memory is needed, an algorithm will be O(1) in memory complexity. – user515430 Oct 12 '14 at 13:58
  • @user515430 No, that’s nonsense. Quicksort emphatically does need non-constant additional space, it requires at least O(log n) additional storage (call stack). The Wikipedia article is wrong on this (they use “in-place” to mean “uses less than O(n) additional storage” — which is okay-ish, I guess. But Quicksort definitely needs more than O(1) additional storage). – Konrad Rudolph Oct 13 '15 at 17:04

1 Answers1

2

For std::sort() I found an answer on Cboard, which pretty much says:

Quality of implementation issue. Different implementations use memory more effectively than other implementations. Beyond that, the standard allows specializations for std::sort for different iterators categories allowing the implementation to choose between a few different options so long as the complexity (time) matches the requirements. The complexity given is not even time, but numbers of comparisons. The implementation could perform N³ swap operations.

The memory overhead for most implementations of std::sort would be related to the depth of recursion and the number of local variables stored onto the stack for each level of recursion. The HP / Microsoft STL implementation of std::sort uses Quicksort until/unless it detects that the recursion level is getting too deep in which case it switches to heap sort. If the size is small, such as 32 or less, then it uses Insertionsort.

You can see a comparison of the algorithms, in the Wikipedia page and estimate the memory complexity.

Similarly, I suspect that the other two algorithms fall into the same case.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    The complexity given is not even time, but numbers of comparisons. The implementation could perform N^3 swap operations. – dyp Oct 09 '14 at 23:16
  • I believe the standard requires `std::sort` to be `O(n*log(n))` in time although I can't quote it right now. – NPS Oct 10 '14 at 00:04
  • I will agree dyp. NPS I fail to find something to support your comment. – gsamaras Oct 10 '14 at 08:33