Thread: memory complexity of std::sort

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    memory complexity of std::sort

    What's the memory complexity of std::sort? I assume O(n), but I can't find any ref for it!

    Also, do I understand the term memory complexity correctly? I understand it as how much memory the algorithm will need to run. So, for example, if std::sort implements an in-place sort, it will take O(n).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Looking at the standard I found this:

    memory complexity of std::sort-sortcomplexity-jpg

    Jim

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What's the memory complexity of std::sort? I assume O(n), but I can't find any ref for it!
    O_o

    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 general implementation is a hybrid burning stack memory from a variation of Quicksort before becoming InsertionSort or similar.

    Also, do I understand the term memory complexity correctly? I understand it as how much memory the algorithm will need to run. So, for example, if std::sort implements an in-place sort, it will take O(n).
    You do not appear to understand.

    The measurement of memory complexity is how many extra slots--each slot as in sizeof(element)--required to perform the operation. The memory use by the collection isn't counted.

    Thus a pure "in-place" algorithm has O(1) memory complexity, but that notation just implies a constant. The actual implementation may use more slots, but the number of slots used remains unrelated to the number of elements.

    With a O(n) memory complexity algorithm, the algorithm requires a number of extra slots equal to the number of elements.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    @jimblumberg:

    Good on you for looking in the standard, but you are looking at one of the member functions of one of the containers.

    Different containers have somewhat different options and requirements for the member function version because the implementation knows more about the characteristics of the implementation than the generic `std::sort' free function may ever know just from inspection the iterator categories.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay what about section 24.4.1, this appears to be the "free" std::sort(), the complexity here is similar to the above.

    memory complexity of std::sort-sortcomp-jpg


    Jim

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Thanks Soma! Jim, isn't that the time complexity? I am interested in the memory complexity!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Thanks Soma!
    ^_^

    Yep.

    Okay what about section 24.4.1, this appears to be the "free" std::sort(), the complexity here is similar to the above.
    @jimblumberg:

    The time complexity is similar. I didn't call your attention to the section because the time complexity was different.

    The other stuff is not necessarily similar. For example, the `std::list::sort' member function is a stable sort while the free `std::sort' is not.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by std10093 View Post
    Jim, isn't that the time complexity? I am interested in the memory complexity!
    The point is: the standard appears to offers no guarantees about the memory complexity (as can be seen from the snippets quoted from the standard), so ask your standards library provider!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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 quick sort 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 insertion sort.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Elysia View Post
    The point is: the standard appears to offers no guarantees about the memory complexity (as can be seen from the snippets quoted from the standard), so ask your standards library provider!
    The complexity given is not even time, but numbers of comparisons.

    @rcgldr, I would agree.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    The complexity given is not even time, but numbers of comparisons.
    That is a form of time complexity. It is evident that std::sort is expected to be implemented by a general purpose comparison based sorting algorithm, so number of comparisons seems to be a reasonable measure for counting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Radix Sort ( + counting sort) does not stable sort.
    By siepet in forum C Programming
    Replies: 6
    Last Post: 11-26-2013, 12:04 PM
  2. Space Complexity of Radix Sort
    By gaurav_13191 in forum C Programming
    Replies: 1
    Last Post: 09-02-2013, 12:09 PM
  3. std::sort with memory?
    By cyberfish in forum C++ Programming
    Replies: 21
    Last Post: 02-22-2008, 02:03 PM
  4. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM
  5. memory Leak - Cannot sort it
    By Venet in forum Windows Programming
    Replies: 7
    Last Post: 08-19-2002, 03:04 AM