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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:08:22+00:00 2026-06-13T04:08:22+00:00

In reading in Chapter 14 of Jon Bentley’s Programming Pearls, 2nd Edition, I understand

  • 0

In reading in Chapter 14 of Jon Bentley’s “Programming Pearls”, 2nd Edition, I understand that heaps use a one-based array and the easiest approach in C is to declare x[n+1] and waste element x[0] (page 148).

On page 157, Jon listed the complete heapsort pseudo code:

for i = [2, n]
    siftup(i)
for (i = n; i >= 2; i--)
    swap(1, i)
    siftdown(i - 1)

Here is an implementation in C. However, the array index starts with 0, instead of 1.

void heapSort(int numbers[], int array_size)
{
  int i, temp;

  // Qiang: shouldn't the stop-condition be i >= 1?
  for (i = (array_size / 2)-1; i >= 0; i--)
    siftDown(numbers, i, array_size);

  for (i = array_size-1; i >= 1; i--)
  {
    // Qiang: shouldn't the swap be done with numbmers[1], instead of numbers[0]?
    temp = numbers[0];
    numbers[0] = numbers[i];
    numbers[i] = temp;
    siftDown(numbers, 0, i-1);
  }
}

void siftDown(int numbers[], int root, int bottom)
{
  int done, maxChild, temp;

  done = 0;
  while ((root*2 <= bottom) && (!done))
  {
    if (root*2 == bottom)
      maxChild = root * 2;
    else if (numbers[root * 2] > numbers[root * 2 + 1])
      maxChild = root * 2;
    else
      maxChild = root * 2 + 1;

    if (numbers[root] < numbers[maxChild])
    {
      temp = numbers[root];
      numbers[root] = numbers[maxChild];
      numbers[maxChild] = temp;
      root = maxChild;
    }
    else
      done = 1;
  }
}

My worry is, if the array starts with index 0, then the following properties will not hold (as written on page 148 in Jon’s book):

leftchild(i) = 2*i
rightchild(i) = 2*i+1
parent(i) = i/2

It looks to me that the properties here only hold when the i starts with 1.

What strikes me is that the analysis part in the implementation used an array starting with index 1, while the implementation part used an array starting with index 0 and didn’t waste the first element.

Am I missing anything here?

Edited
With help from interjay, I realized the error contained in the original implementation, which could be shown with a testing input array of {66,4,23,4,78,6,44,11,22,1,99}.

Changed the original siftDown() function a little bit to adjust the relationship between the index of parent and those of its children:

void siftDown(int numbers[], int root, int bottom)
{
  int done, maxChild, temp;

  done = 0;
  while ((root*2 + 1 <= bottom) && (!done))
  {
    if (root*2 + 1 == bottom ||
        numbers[root * 2 + 1] > numbers[root * 2 + 2])
      maxChild = root * 2 + 1;
    else
      maxChild = root * 2 + 2;

    if (numbers[root] < numbers[maxChild])
    {
      temp = numbers[root];
      numbers[root] = numbers[maxChild];
      numbers[maxChild] = temp;
      root = maxChild;
    }
    else
      done = 1;
  }
}

Credits go to interjay, 🙂

Afterword:
It looks the same error doesn’t appear in the implementations in wikibooks and algorithmist. Hooray!

  • 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-13T04:08:23+00:00Added an answer on June 13, 2026 at 4:08 am

    The heap elements can be stored starting with index 0 or index 1, the decision on which to use is up to you.

    If the root element is at index 1, the mathematical relations between parent and child indices are simple as you’ve shown above, and for that reason many books choose to teach it that way.

    If the root is at index 0, you’d get these relations instead:

    leftchild(i) = 2*i+1
    rightchild(i) = 2*i+2
    parent(i) = (i-1) / 2
    

    It doesn’t matter which one you pick as long as you are consistent.

    The C code you’ve shown seems incorrect to me. It starts with array index 0, but uses the parent/child relations appropriate for starting with index 1.

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

Sidebar

Related Questions

I am reading the chapter function templates of C++ Primer 3rd edition , and
While reading SICP I came across logic programming chapter 4.4. Then I started looking
I am reading Programming In Haskell, in the 8th chapter, the author gives an
Guys from book C++ GUI programming with qt I'm reading a chapter on how
So I'm reading the security chapter of Symfony2 Book. I understand everything, but I'd
Am reading an O'Reilly book called Learning WCF and in chapter one, it mentions:
Im reading Jon Skeet book. ( Expression Trees Chapter) It has an example of
I am reading the chapter 16.4 of Programming in Lua and I can't get
I'm reading the LYAH chapter on applicative functors, and I don't seem to understand
I was reading in Practical Clojure (Chapter 5) that the rseq function operation executes

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.