public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "allachan at au1 dot ibm.com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug manual/10672] qsort may not be a stable sort under memory exhaustion
Date: Tue, 11 Mar 2014 07:07:00 -0000	[thread overview]
Message-ID: <bug-10672-131-5M7gdwuFkM@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-10672-131@http.sourceware.org/bugzilla/>

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.


  parent reply	other threads:[~2014-03-11  7:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-10672-131@http.sourceware.org/bugzilla/>
2011-10-23  8:17 ` bugdal at aerifal dot cx
2012-12-19 10:47 ` schwab@linux-m68k.org
2014-03-11  7:07 ` allachan at au1 dot ibm.com [this message]
2014-07-01  6:49 ` fweimer at redhat dot com
2014-07-01  7:05 ` andersk at mit dot edu
2014-07-01  8:11 ` fweimer at redhat dot com
2014-07-01  8:15 ` andersk at mit dot edu
2014-12-10 15:50 ` neleai at seznam dot cz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-10672-131-5M7gdwuFkM@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).