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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:43:50+00:00 2026-05-15T05:43:50+00:00

I have an operation on a heap, a fixdown operation . This is the

  • 0

I have an operation on a heap, a fixdown operation . This is the code:

public class Heap {

    public static void fixdown (int a[],int k,int n) {
        while (2*k<=n) {
            int j=2*k;
            if (j<n && a[j]<a[j+1]) j++;
            if (!(a[k]<a[j])) break;
            swap(a,k,j);
            k=j; 
        }
    }

    public  static void main (String[]args) {
        int a[]=new int[]{12,15,20,29,23,22,17,40,26,35,19,51};
        fixdown(a,1,a.length);
        for (int i=0;i<a.length;i++) {
            System.out.println(a[i]);
        }
    }

    public static void swap (int a[],int i,int j) {
        int t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
}

Update: I have changed it and now there is no error.

//result is

12
29
20
40
23
22
17
15
26
35
19
51
  • 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-15T05:43:52+00:00Added an answer on May 15, 2026 at 5:43 am

    Those lines:

    for (int i=0;i<a.length;i++){
     System.out.println(a[i]);
    

    suggest, that indexes in Your array are 0 based. If so, the indexes of the left and right child of the element at index i should be calculated as

    leftchild_index = 2*i+1;
    rightchild_index = 2*i+2; // rightchild_index = leftchild_index + 1
    

    left child of a[0] is a[1] and the right one is a[2]

    If parameter n is the length of an array containing the heap, You need to fix some conditions

    while(2*k<=n){
    

    should be changed to:

    while(2*k + 1 < n){
    

    and

    int j=2*k;
        if (j<n && a[j]<a[j+1])   j++;
    

    should be changed to

    int j = 2 * k + 1;
        if (j < n - 1 && a[j] < a[j+1])   j++;
    

    Otherwise You are accessing the array out of bounds.

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

Sidebar

Related Questions

No related questions found

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.