Skip to content

Add heapsort fallback in select_nth_unstable#106997

Merged
bors merged 1 commit intorust-lang:masterfrom
Sp00ph:introselect
Jan 18, 2023
Merged

Add heapsort fallback in select_nth_unstable#106997
bors merged 1 commit intorust-lang:masterfrom
Sp00ph:introselect

Conversation

@Sp00ph
Copy link
Copy Markdown
Member

@Sp00ph Sp00ph commented Jan 17, 2023

Addresses #102451 and #106933.

slice::select_nth_unstable uses a quick select implementation based on the same pattern defeating quicksort algorithm that slice::sort_unstable uses. slice::sort_unstable uses a recursion limit and falls back to heapsort if there were too many bad pivot choices, to ensure O(n log n) worst case running time (known as introsort). However, slice::select_nth_unstable does not have such a fallback strategy, which leads to it having a worst case running time of O(n²) instead. #102451 links to a playground which generates pathological inputs that show this quadratic behavior. On my machine, a randomly generated slice of length 1 << 19 takes ~200µs to calculate its median, whereas a pathological input of the same length takes over 2.5s. This PR adds an iteration limit to select_nth_unstable, falling back to heapsort, which ensures an O(n log n) worst case running time (introselect). With this change, there was no noticable slowdown for the random input, but the same pathological input now takes only ~1.2ms. In the future it might be worth implementing something like Median of Medians or Fast Deterministic Selection instead, which guarantee O(n) running time for all possible inputs. I've left this as a FIXME for now and only implemented the heapsort fallback to minimize the needed code changes.

I still think we should clarify in the select_nth_unstable docs that the worst case running time isn't currently O(n) (the original reason that #102451 was opened), but I think it's a lot better to be able to guarantee O(n log n) instead of O(n²) for the worst case.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants