Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:03:17+00:00 2026-06-12T17:03:17+00:00

I am trying to learn and implement a min-heap to solve a problem: Loop

  • 0

I am trying to learn and implement a min-heap to solve a problem: Loop that creates doubles and insert them into sorted array, in C

Basically, I start with a set of doubles, sorted from smallest to largest. Then, I generate doubles (possibly at random) and have to add the newly generated doubles to the set while keeping it sorted. Also, each time I insert a double, I remove the smallest double from the set.

(edit – The set doesn’t have to be entirely sorted. The goal is to be able to look up and remove the minimum element after every insertion of a double. Keeping the set sorted was my first, naive, solution.)

Sounds like the kind of thing a min-heap was made to do.

Attempt

Since in C, array sizes are declared in advance, I have to create an array with length = maximum number of doubles I will end up with. Then, fill all the entries of this array with the value max_double.

Using the methods described here as a guide: http://opendatastructures.org/versions/edition-0.1e/ods-java/10_1_BinaryHeap_Implicit_Bi.html, I can make functions to insert and remove values into this array.

Insert: Replace the last entry (which will always be max_double) of the array with the number. Then keep swapping with the value of the parent node until the parent node’s value is less than newly added value.

Remove: Replace the root node with max_double, and then compare it with its two children, swapping with the least of the children until its two children are max_double.

Question

Am I using the right approach for all these?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:03:18+00:00Added an answer on June 12, 2026 at 5:03 pm

    A heap will not make the array sorted. It simply follows the rules, meaning that in a min heap, the parent will always be smaller then his children. Unfortunately, the children do not have to be in order.

    If you want to implement a heap in an array, the wiki has a great page about it.

    http://en.wikipedia.org/wiki/Binary_heap

    Some more explanation:

    Insert :
    Add the element to the end of the array (no replacing!)
    Compare the element with the parent at(i-1/2) if the order is correct stop.
    Otherwise swap and do step 2 again. (remember to update the current index)

    Delete :
    Swap min with last element in the array
    Delete the last element
    Now going from the top, compare the root with its children, swap with the smallest, otherwise stop.
    Keep comparing with the children until the condition is satisfied or you don’t have any other children.

    For index starting at 0, the parent is at floor((i-1)/2) and children are at 2i+1 and 2i+2

    One more thing, i suggest using a deque to store your doubles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to implement the more command. I want to learn that how
I am trying to learn Objective-C and have run into a problem with pushing
I am trying to learn Java. I would like to implement a simple networked
I am trying to learn LINQ to SQL. I have successfully implemented insert method
im trying to learn delegates and events in c#, i understand that an event
I'm trying to learn Java EE 6 and i'm just wondering how to implement
I am trying to learn more about BindingList because I believe that it will
I'm a designer trying to learn Jquery and I'm having a problem with a
While trying to learn c++, I tried to implement class representing very basic trie.
I am trying to learn how to implement Web Services using Axis2 and Tomcat,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.