Skip to content

qsort implementation to minimal stdlib #29804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

JonBruchim
Copy link

added musl implementation of qsort
http://git.musl-libc.org/cgit/musl/tree/src/stdlib/qsort.c

related to this issue i've opened
#28896

Signed-off-by: Jonathan Bruchim [email protected]

@github-actions github-actions bot added the area: C Library C Standard Library label Nov 4, 2020
Copy link
Contributor

@andrewboie andrewboie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation from musl seems reasonable at first glance.

The license may be a problem.

It would be good to have a test, although I am not completely sure what the policy is for minimal libc as I know we don't have full test coverage, and since it implements a standard I think the direction was to find third-party test suite. But we have at least some tests in tests/lib/c_lib.

Probably need some input from the TSC on this.

@andrewboie andrewboie added the TSC Topics that need TSC discussion label Nov 5, 2020
@github-actions
Copy link

github-actions bot commented Jan 5, 2021

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@cfriedt
Copy link
Member

cfriedt commented Jan 11, 2021

I have some reservations about just copying from another project, although I haven't looked at the implementation.

  1. qsort does not actually specify which sorting algorithm should be used under the hood, which leaves a lot of possibilities open for the implementation. I've read that musl has implemented Smoothsort
  2. if we add a sorting algorithm to the minimal libc, let's add one that has decent worst-case performance.
  3. if we add a sorting algorithm to the minimal libc, let's add one that has all of the other random features that we could want, such as
  • in place sorting (no external memory required)
  • stability (i.e. the input order of elements is preserved in the output)

A decent reference for some of these features are here:
https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms
https://en.wikipedia.org/wiki/Qsort

I mistakenly opened #31107 for this same topic since I've been carrying qsort.c in my greybus module for a while (from the BSD C library). However, due to the aforementioned concerns, I never bothered to make a PR.

@JonBruchim - would you be able to provide some analysis of this vs possibly other algorithms?

Just at a quick glance, Block Sort looks promising too.

https://en.wikipedia.org/wiki/Block_sort

  • O(N) best case
  • O(NlogN) worst / average case
  • O(1) space
  • Stable
  • In-place

I haven't looked to see if it can be implemented easily though, or what kind of stack space it might require. That's also obviously a concern.

I guess the only slight advantage that Block Sort might have is that it's stable. Smoothsort looks to have a reasonably simple implementation though.

@cfriedt cfriedt self-requested a review January 11, 2021 22:28
@github-actions github-actions bot removed the Stale label Jan 12, 2021
@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Mar 22, 2021
@github-actions github-actions bot closed this Apr 6, 2021
@cfriedt cfriedt removed the TSC Topics that need TSC discussion label Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants