[PATCH v2 0/2] stdlib: Optimize number of calls to comparison function in qsort

Kuan-Wei Chiu visitorckw@gmail.com
Tue Dec 5 03:22:05 GMT 2023


The current heapsort implementation in the siftdown function follows
the standard textbook version, necessitating two comparisons at each
level. Transitioning to the Bottom-up heapsort version allows us to
decrease the required comparisons to just one per level. On average,
this modification significantly reduces the comparison count from
2nlog2(n) - 3n + O(n) to nlog2(n) + 0.37*n + O(n).

Refs:
  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://doi.org/10.1016/0304-3975(93)90364-Y

Changes from v1:
* Use a more concise title.
* Correct the error in asymptotic notation from little-o to big-O.
* Adjusted the factor in tst-qsort5 from 4.5 to 3.5.

Kuan-Wei Chiu (2):
  stdlib: Optimize number of calls to comparison function in qsort
  stdlib: Adjust the factor in tst-qsort5

 stdlib/qsort.c      | 36 ++++++++++++++++++------------------
 stdlib/tst-qsort5.c |  2 +-
 2 files changed, 19 insertions(+), 19 deletions(-)

-- 
2.25.1



More information about the Libc-alpha mailing list