This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v2] manual: Remove incorrect claim that qsort() can be stabilized
- From: Anders Kaseorg <andersk at MIT dot EDU>
- To: Florian Weimer <fweimer at redhat dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Wed, 2 Jul 2014 21:17:50 -0400 (EDT)
- Subject: [PATCH v2] manual: Remove incorrect claim that qsort() can be stabilized
- Authentication-results: sourceware.org; auth=none
- References: <alpine dot DEB dot 2 dot 02 dot 1407010413160 dot 28890 at all-night-tool dot MIT dot EDU> <53B27423 dot 2050903 at redhat dot com>
Under certain conditions on the size of the array and its items,
qsort() may fall back to an in-place quicksort if it cannot allocate
memory for a temporary array with malloc(). This algorithm is not a
stable sort even if the comparison function is written in the
described manner.
Fixes #10672.
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
---
On Tue, 1 Jul 2014, Florian Weimer wrote:
> I would suggest to remove the entire paragraph instead, or perhaps a warning
> that the pointers with which the comparison function is called do not
> necessarily have to reside within the input array.
What do you think about this wording, then? (The problem is not actually
about pointers residing outside the input array, because it happens that
the algorithm where that occurs is stable. But I agree itâs worth
mentioning too.)
Anders
manual/search.texi | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/manual/search.texi b/manual/search.texi
index 509a543..8aff574 100644
--- a/manual/search.texi
+++ b/manual/search.texi
@@ -180,11 +180,10 @@ This can make a difference when the comparison considers only part of
the elements. Two elements with the same sort key may differ in other
respects.
-If you want the effect of a stable sort, you can get this result by
-writing the comparison function so that, lacking other reason
-distinguish between two elements, it compares them by their addresses.
-Note that doing this may make the sorting algorithm less efficient, so
-do it only if necessary.
+The addresses passed to the comparison function need not correspond with
+the original location of the objects, and need not even lie within the
+original array. The only way to perform a stable sort with @var{qsort}
+is to first augment the objects with a monotonic counter of some kind.
Here is a simple example of sorting an array of doubles in numerical
order, using the comparison function defined above (@pxref{Comparison
--
2.0.1