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

ENH: added selectperm and selectperm! based on new PartialQuickSort #10800

Closed
wants to merge 2 commits into from

Conversation

sglyon
Copy link
Contributor

@sglyon sglyon commented Apr 13, 2015

This pull request implements 2 main things:

  • a PartialQuickSort(k) algorithm based on the work by @carlobaldassi in this gist
  • Functions selectperm / selectperm! that are to sortperm / sortperm! what select / select! are to sort / sort!

I have tried to add minimal tests and docs. I don't remember how to add things to the helpdb, so the entries from the sort.rst will not appear at the REPL.

Some things can certainly be cleaned up. Comments welcome.

closes #10767

@sglyon sglyon mentioned this pull request Apr 13, 2015
@sglyon sglyon changed the title ENH: added selectperm and selectperm! based on new PartialQuickSort algo... ENH: added selectperm and selectperm! based on new PartialQuickSort Apr 13, 2015
@ViralBShah
Copy link
Member

Cc: @kmsquire

@@ -1,5 +1,4 @@
export
# Modules
export # Modules
Copy link
Member

Choose a reason for hiding this comment

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

unintentional change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I'll revert that line

end

# partially sort until the end of the range
PartialQuickSort(r::OrdinalRange) = PartialQuickSort(last(r))
Copy link
Member

Choose a reason for hiding this comment

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

If we're going to include this, would it be good to support arbitrary ordinal ranges?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand what you mean by arbitrary ordinal range, but I think that kinda happens when we use this algorithm on 443 inside selectperm!. There I say sort until the end of the range and then only return sorted indices within the range (return ix[k])

I agree it would probably be better to support this in the sort algorithm directly, but I didn't see an obvious way to extend @carlobaldassi code to handle it there.

@kmsquire kmsquire self-assigned this Jun 26, 2015
@sglyon
Copy link
Contributor Author

sglyon commented Jul 4, 2015

Ping.

@kmsquire I see that you have been self-assigned to take care of this. How can I help?

@kmsquire
Copy link
Member

kmsquire commented Jul 5, 2015

Bumping was good. :-) let me take a closer look at the PR.


# TODO: Not type stable. If k is an int, this will return an Int, of it is
# an OrdinalRange it will return a Vector{Int}. This, however, seems
# to be the same behavior as as `select`
Copy link
Member

Choose a reason for hiding this comment

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

Actually, your description shows that this function is type-stable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha yes it sure is. Sorry for the inaccurate comment there.

@kmsquire
Copy link
Member

kmsquire commented Jul 6, 2015

Right now, PartialQuickSort pretty much duplicates an older version of QuickSort.

Duplication is sometimes okay and sometimes necessary, but in this case, it would probably be better to refactor and reuse parts of QuickSort. In particular, QuickSort has an improved pivot selection (at little or no cost), as well as O(n log n) worst-case performance (PartialQuickSort has neither).

I've already worked through part of the refactor (mostly to make sure it made sense). I can submit that as a pull request to this patch, or as a separate PR (and close this one), or I can help you work through it if you'd like, @spencerlyon2.

I'll add a couple of more comments inline.

sort!(v, lo, j, PartialQuickSort(k), o)
end
end
jk = min(j, lo + k - 1)
Copy link
Member

Choose a reason for hiding this comment

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

This isn't used/needed.

@sglyon
Copy link
Contributor Author

sglyon commented Jul 6, 2015

@kmsquire -- thanks for all the notes. Very helpful. Sorry the PR was a bit sloppy. I was mostly just pulling code from a few different sources to jump start conversation about having this functionality in base.

I don't think there is any reason to try to fix this PR up if you already have a working version that leverages QuickSort. My vote would be to use your routine if you are ok with that.

@kmsquire
Copy link
Member

Just a comment that I have been working on this, and will submit a WIP PR soon.

@kmsquire
Copy link
Member

Included in #12303.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

selectperm?
5 participants