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

Why SortedCollection sort: and sortBlock: do not uses the same method to sort? #2866

Closed
MarcusDenker opened this issue Mar 18, 2019 · 3 comments

Comments

@MarcusDenker
Copy link
Member

from: https://pharo.fogbugz.com/f/cases/17925/Why-SortedCollection-sort-and-sortBlock-do-not-uses-the-same-method-to-sort

sortBlock: uses SortedCollection>>sort:to: algorithm (kind of merge sort) and sort: the mergeSort on Array.

@kasperosterbye
Copy link
Contributor

kasperosterbye commented Sep 13, 2019

It seems even more odd than what Marcus describes. The #sort: method is implemented in these collection classes:

SequenceableCollection -> uses its own implementation of mergesort
OrderedCollection -> delegates to the mergesort array inherits from Sequencable collection
SortedCollection -> deletates to the algorithm in SequenceableCollection
LinkedList -> uses the same as the above
Heap -> One whould think that heap used heap sort, but it seems to use a COPY of the mergesort found in SequenceableCollection.

Finally, as Marcus noticed, the #sortBlock: of SortedCollection uses a #sort:to: mergesort implemented in SortedCollection. The clue in the sort:to: seems to be that they know they use an array as datastructure, and can use swap and other things.

I made some timings of the two algorithms, and it seems like the #sort:to: is a few (2-3) percent faster, so not really an issue.

My suggestion is to remove the marginally faster algorithm, and make all build-in sorting be based on the algorithm in SequenceableCollection

I tried, and it does not violate any tests.

@kasperosterbye
Copy link
Contributor

It is actually worse than bad.
The implementation used everywhere but in SortedCollection is a conservative sort (keeping equal elements in original order).
The adding of new elements into a SortedCollection is linear (uses insertion by moving existing elements).

However, because the browser - Calypso - depends so strongly on SortedCollection the Pharo system hangs if I try to change anything in SortedCollection.

@MarcusDenker MarcusDenker removed the Easy label Feb 10, 2022
@guillep
Copy link
Member

guillep commented Jan 27, 2023

This is more a discussion than an actionable issue.
Please reopen if you consider there is an objective way to fix it.

@guillep guillep closed this as completed Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants