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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:13:53+00:00 2026-05-26T02:13:53+00:00

This might be a weird question but I’m trying to figure out why the

  • 0

This might be a weird question but I’m trying to figure out why the following code works.

It seems as though this code should be used where the heap elements index starts at 1 but it’s starting at 0 and the sort is completed correctly.

I say that because the left child is calculated as (element*2) and the right child is (element*2 + 1). This would make the left child for the element with index 0 also have index 0.

#include <iostream>
using namespace std;

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;
        }
    }
}

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

    for (i = n/2; i >= 0; i--) {
        siftDown(numbers, i, n - 1);
    }

    for (i = n-1; i >= 1; i--) {
        temp = numbers[0];
        numbers[0] = numbers [i];
        numbers [i] = temp;
        siftDown(numbers, 0, i-1);
    }
}

int main() {
    int cases;
    int n;
    int count;

    cin >> cases;

    for (int i=0; i < cases; i++) {
        cin >> n;
        int array[n];
        for (int j=0; j < n; j++) {
            cin >> array[j];
        }

        heapSort(array, n);
        for (int k=0; k < n; k++) {
            cout << array[k];
        }    
        cout << endl;
    }
}
  • 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-05-26T02:13:54+00:00Added an answer on May 26, 2026 at 2:13 am

    For the case when root = 0, there are two sub-cases: numbers[0] > numbers[1], or not. In the first, maxchild is set to 0. The next “if” clause – effectively “numbers[0] < numbers[0]” – necessarily evaluates to false, and so “done” is set to 1 and the loop terminates.

    If numbers[1] >= numbers[0], then maxchild is set to 1. The next clause becomes “numbers[0] < numbers[1]” which may be true or may be false if numbers[0] == numbers[1]. If it is false, the loop terminates as before. If it is true, numbers[0] and numbers[1] are swapped – thus the larger number correctly moves to the top of the heap – and root becomes 1, the loop continues and in this case you understand how it works.

    I think it’s easiest to consider this case as a heap where the root only has one child (and all other nodes have two children as normal).

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

Sidebar

Related Questions

This might seem like a weird question, but bear with me. I'd like to
This might seems silly question but for me it is annoying. I have installed
This might be a weird question, but I'll try anyway. I set up my
This might be a weird question. After all, most people want to jump OUT
I feel this might be a weird/stupid question, but here goes... In the question
This might be a weird question, but I know what I'm looking for i
Hey guys, this question might be a little weird worded but I was looking
Ok, this might be a weird request, but is it possible to essentially flatten
This might seem a bit weird, but since I am new in RoR and
This might be a silly question, but I want to ask if there is

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.