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

Heapsort inefficiencies #28

Open
RyanNaughton opened this issue Oct 20, 2015 · 1 comment
Open

Heapsort inefficiencies #28

RyanNaughton opened this issue Oct 20, 2015 · 1 comment

Comments

@RyanNaughton
Copy link

One of the key advantages of heap sort is it can be an in place algorithm (https://en.wikipedia.org/wiki/In-place_algorithm); however, your implementation does not do an in place implementation:

https://github.com/kanwei/algorithms/blob/master/lib/algorithms/sort.rb#L85-L90

Instead, you are popping the values off the heap and putting them in a new array (when they could be kept in that array but moved to the end). Instead of calculating the size of the array in the heap, you could keep an index for the end of the heap within the array, and thus move the sorted values that have been popped off to the end of the actual array and decrement the heap's index.

At a minimum, the array here could be instantiated with the known size of the container. That prevents the array from being resized repeatedly (Ruby resizes an array when it hits 3, 20, 37, 56, 85, ... sizes, see here: https://github.com/ruby/ruby/blob/ruby_2_0_0/array.c#L183)

@bcc32
Copy link

bcc32 commented Dec 30, 2023

The Heap implementation has been replaced with a Fibonacci heap, rather than an array-based binary heap, at this point. I think, if the heapsort were ever to be implemented in-place, it'd have to be a separate heap implementation now.

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

No branches or pull requests

2 participants