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

Add an option to relax the success condition to yielding a heap. #13

Open
sh1boot opened this issue Jun 14, 2024 · 1 comment
Open

Add an option to relax the success condition to yielding a heap. #13

sh1boot opened this issue Jun 14, 2024 · 1 comment

Comments

@sh1boot
Copy link

sh1boot commented Jun 14, 2024

I was just wondering if make_heap() could benefit from implementation as a sorting network in some cases, as the preamble to various other algorithms adjacent to sorting.

So I guess the first thing to know is how much smaller and shorter the network can be compared to a full sort. Is that a thing worth implementing?

Seems like a thing I should just try for myself, but I'm halfway down a whole other rabbit hole right now and I just wanted to get this written down before I forgot.

@bertdobbelaere
Copy link
Owner

I think that creating a heap of n elements could be done with a trivial network of n-1 elements. In pseudocode:
for k from n-1 downto 1:
add_element(k, floor((k-1)/2))
e.g. for n=10 you get the network (9,4),(8,3),(7,3),(6,2),(5,2),(4,1),(3,1),(2,0),(1,0)
The output is now a heap, and it is optimal because with less elements (n-2) the network is no longer connected. So actually the network is just another representation of what make_heap() already does.

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