This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug manual/10672] qsort may not be a stable sort under memory exhaustion


https://sourceware.org/bugzilla/show_bug.cgi?id=10672

paxdiablo <allachan at au1 dot ibm.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |allachan at au1 dot ibm.com

--- Comment #3 from paxdiablo <allachan at au1 dot ibm.com> ---
It's a well known trick that you can make an unstable sort like Quicksort into
a stable one by adding an address to the comparison key as the most minor
component.

However, this must be the STARTING address of each element and must be
populated before the sort proper starts with something like:

for (int i = 0; i < sz; i++) elem[i].startaddr = &(elem[i]);

By using the starting address as the most minor part of the key, the order of
otherwise similarly-keyed elements will be preserved.

Using the TRANSITORY address will not work and in fact will break the sort
contract as sometimes a will be less than b and sometimes vice versa, depending
on their current address in memory. So, yes, that section of the documentation
is dead wrong and should be removed or fixed.

And, in fact, qsort() is not REQUIRED to be stable as per the ISO C standard.
If you WANT a stable sort, go grab a copy of Mergesort from somewhere.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]