Skip to content
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

[SR-306] Add stable sort algorithm #42928

Closed
gribozavr opened this issue Dec 18, 2015 · 9 comments
Closed

[SR-306] Add stable sort algorithm #42928

gribozavr opened this issue Dec 18, 2015 · 9 comments
Labels
feature A feature request or implementation good first issue Good for newcomers improvement standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

Comments

@gribozavr
Copy link
Collaborator

Previous ID SR-306
Radar rdar://problem/18221680
Original Reporter @gribozavr
Type Improvement
Additional Detail from JIRA
Votes 5
Component/s Standard Library
Labels Improvement, NewAPIRequest, StarterBug, StarterProposal
Assignee None
Priority Medium

md5: 37f09a79466d145bd076877be8089150

Issue Description:

The standard library should provide a stable sorting algorithm, next to the regular, unstable, sort() and sortInPlace().

This needs a proposal to swift-evolution (the design should be trivial, new methods should mirror existing sorting APIs), and implementation. The standard library already contains an internal implementation of insertion sort, so implementing this should amount to providing public entry points and writing tests.

@jtbandes
Copy link
Collaborator

Is the goal to provide API separately like stableSort[InPlace](), or to have optional parameters on the existing functions, like sort[InPlace](stable: true)?

@gribozavr
Copy link
Collaborator Author

That would be a good point to discuss on swift-evolution.

@swift-ci
Copy link
Collaborator

Comment by Félix Fischer (JIRA)

Is this dropped? It looks dropped.

I could take this one... if you guys don't mind and still consider it helpful 🙂

Although... it probably needs to pass through swift-evolution first. Hmm...

Also, this is for the StdLib, so someone has already probably implemented a good and stable algorithm in the way that would be needed. After all, it's Swift. People have done many things already with it xD

@swift-ci
Copy link
Collaborator

Comment by Raphael (JIRA)

@gribozavr I hope you're not advocating for Insertion Sort as general-purpose sorting algorithm!

@swift-ci
Copy link
Collaborator

Comment by Félix Fischer (JIRA)

He probably isn't XD

Also, we could probably use something like this. That way, we'd base the stable sort in the already-tested, fast af Sort in the StdLib, without losing much performance or fiddliness.|https://stackoverflow.com/questions/1427608/fast-stable-sorting-algorithm-implementation-in-javascript].]

I'm gonna check the source code, and if this is possible, I'll send an email to Swift Evolution

@swift-ci
Copy link
Collaborator

Comment by Raphael (JIRA)

felix91gr (JIRA User) Copying arrays an additional two times? Hm. Why not just use good old Mergesort? There are also in-place stable sorting algorithms. Nothing new to invent.

@swift-ci
Copy link
Collaborator

Comment by Félix Fischer (JIRA)

Because Quicksort (and in general, any algorithm that uses it) is very good at using the cache. How are the Block Sorts in this regard? If they are comparable, marvelous: they should be included in the StdLib. But if they aren’t, like HeapSort, I’d consider another approach, even if it took O👎 extra memory to be run.

@swift-ci
Copy link
Collaborator

Comment by Raphael (JIRA)

True, Quicksort is that. As these things go, we'll have to pay something for stability. You seem to be saying, "let's only include such an algorithm if it's as fast as the unstable sort"; I don't think a solution lies that way.

I have not looked into it in depth, but my gut feeling is that bottom-up Mergesort should be reasonably cache-efficient. Maybe the trickery to make it work in-place hurts this, I'm not sure. Research literature on this exists (of course), but I don't have the time to dig through it.

Why would you expect extra memory use to improve cache efficiency? Seems unlikely to me: "new" memory will always provoke cache misses.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

Proposal: SE-0372

@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented and removed API request labels Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation good first issue Good for newcomers improvement standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented
Projects
None yet
Development

No branches or pull requests

4 participants