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 8762571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:33:43+00:00 2026-06-13T15:33:43+00:00

Here we have a snippet of an array implementation of a binary heap. I

  • 0

Here we have a snippet of an array implementation of a binary heap. I would like some help seeing what this for loop means in pseudocode:

public void insert (Anytype x) { 
    int hole = ++currentSize; //currentSize is the size of the array
    for (array[0] = x; x.compareTO(array[hole / 2]) < 0; hole /= 2)
        array[hole] = array[hole / 2];
    array[hole] = x;
}

I can’t seem to understand how this for loop works. Thank you.

EDIT filled the hole

  • 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-13T15:33:44+00:00Added an answer on June 13, 2026 at 3:33 pm

    An array converted into a binary heap can be seen as follows

             [elem 0] <-- put the inserted element here? (why? a precaution perhaps?)
    
             [element 1]...
        [element2] [ element3 ]  
    [elem4][elem5] [elem6][elem7]  
    [x][x] [x][x]  [x][XX] ... <-- unoccupied
    

    The code travels to the parent of each node by dividing the index hole by 2.
    Then it moves the parent up to the current node, if parent > current node.

    There’s a mistake I think… At least this is not the typical solution, where one puts the inserted element as the last non occupied ‘hole’ and travels up from that position….

    The correct method is just start comparing the parent of the last element and iterate towards root. There is no need to swap, but where ever index ‘hole’ ends, that’s the proper place to finally put the new element. (The typical solution puts ‘X’ in the last position and swaps, but that is inefficient. ) Also the first initialization in the for loop is unnecessary.

    Anyway, the index arithmetic works when index 0 is omitted.

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

Sidebar

Related Questions

I have a snippet of code like this: var profileLinks = new Array(); for
I have this snippet where I am trying to generate array of array of
I have an array in PHP, here is a snippet: $locations = array( array(
I have a dataset obtained from MySQL that goes like this: Array ( [0]
I have a small code snippet for deleting element in linked list. Here is
I have a class with two constructors (C#). Here is the code snippet: public
Here I have an arbitrary IEnumerable<T> . And I'd like to page it using
I have an array being returned from the database that looks like so: $data
I was wondering if this code snippet is considered legal: $arr = array(123,456,789,123,456,789); foreach($arr
I have this snippet of code which I am considering to simplfy: if (numberOfResults

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.