Re: [PATCH] Implement introsort for qsort

[email protected]
Newsgroups gmane.comp.lib.newlib
Organization Systematic Software
Message-ID <[email protected]>
On 2023-12-30 12:59, Kuan-Wei Chiu wrote:
> Enhances the qsort implementation by introducing introsort to address
> the worst-case time complexity of O(n^2) associated with quicksort.
> Introsort is utilized to switch to heapsort when quicksort recursion
> depth becomes excessive, ensuring a worst-case time complexity of
> O(n log n).
> 
> The heapsort implementation adopts a bottom-up approach, significantly
> reducing the number of required comparisons and enhancing overall
> efficiency.
> 
> Refs:
>    Introspective Sorting and Selection Algorithms
>    David R. Musser
>    Software—Practice & Experience, 27(8); Pages 983–993, Aug 1997
>    https://dl.acm.org/doi/10.5555/261387.261395
> 
>    A killer adversary for quicksort
>    M. D. McIlroy
>    Software—Practice & Experience, 29(4); Pages 341–344, 10 April 1999
>    https://dl.acm.org/doi/10.5555/311868.311871
> 
>    BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT beating, on an average,
>    QUICKSORT (if n is not very small)
>    Ingo Wegener
>    Theoretical Computer Science, 118(1); Pages 81-98, 13 September 1993
>    https://dl.acm.org/doi/10.5555/162625.162643
> 
> Signed-off-by: Kuan-Wei Chiu <[email protected]>
> ---
> To assess the performance of the new introsort and the old quicksort in
> worst-case scenarios, we examined the number of comparisons required
> for sorting based on the paper "A killer adversary for quicksort."
> 
> Before the patch:
> n = 1000,  cmp_count = 100853
> n = 2000,  cmp_count = 395991
> n = 3000,  cmp_count = 885617
> n = 4000,  cmp_count = 1569692
> n = 5000,  cmp_count = 2448183
> n = 6000,  cmp_count = 3521140
> n = 7000,  cmp_count = 4788493
> n = 8000,  cmp_count = 6250342
> n = 9000,  cmp_count = 7906610
> n = 10000,  cmp_count = 9757353
> 
> After the patch:
> n = 1000,  cmp_count = 27094
> n = 2000,  cmp_count = 61500
> n = 3000,  cmp_count = 100529
> n = 4000,  cmp_count = 136538
> n = 5000,  cmp_count = 182698
> n = 6000,  cmp_count = 221120
> n = 7000,  cmp_count = 259933
> n = 8000,  cmp_count = 299058
> n = 9000,  cmp_count = 356405
> n = 10000,  cmp_count = 397723

See an earlier post on the same issue:

https://inbox.sourceware.org/newlib/[email protected]/

which addresses points your patch addresses, with more analysis, but no data or 
tests, and was not reworked or resubmitted as a valid patch.

You may also wish to look at the BSD implementations for comparison to see how 
they may have addressed these issues, and replace current newlib with an update 
from another distro, that has already received live testing.

Evaluating sort implementations requires you to also look at number of moves as 
well as comparisons - reducing comparisons is only useful if that results in 
fewer data moves - in general doing (not too many) more comparisons to make 
fewer moves gives better performance, so you have to check that, and also 
measure stack and memory used, as newlib may be built for some low memory 
targets, which may need some light sorting done, but not at the expense of 
(much) more memory or time used.

So you also have to compare behaviour of different test data, including all rows 
identical, already in forward and in reverse order, two values out of order 
repeated to fill the array, in alternating rows and alternate halves, and other 
documented antagonistic sequences, with pre- and post-change counts and times, 
to check there are no data-dependent regressions in time taken or space used.

You may want to make your improved sort implementation available in a way that 
allows easily switching it in and out, using definitions or config options, so 
that you, or other newlib users, can easily do their own comparisons.

-- 
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retirer     but when there is no more to cut
                                 -- Antoine de Saint-Exupéry
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.