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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:22:36+00:00 2026-06-08T05:22:36+00:00

I am trying to create a Max-Heap in java using the following code: public

  • 0

I am trying to create a Max-Heap in java using the following code:

public class Heapify {
// 16 14 10 8 7 9 3 2 4 1

    public static int[] Arr = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
    public static int counter = 0;

    public static void main(String[] args) {
        int kk;
        for (kk = 0; kk <= Arr.length - 1; kk++) {
            heapM(Arr, kk);
        }

        for (int krk = 0; krk < Arr.length; krk++) {
            System.out.println(Arr[krk]);
        }



    }

    public static void heapM(int[] Arr, int i) {

        int largest;
        int left = i * 2;
        int right = i * 2 + 1;
        if (((left < Arr.length) && (Arr[left] > Arr[i]))) {
            largest = left;
        } else {
            largest = i;
        }

        if (((right < Arr.length) && (Arr[right] > Arr[largest]))) {
            largest = right;
        }
        if (largest != i) {
            swap(i, largest);


            heapM(Arr, largest);
        }
    }

    private static void swap(int i, int largest) {
        int t = Arr[i];
        Arr[i] = Arr[largest];
        Arr[largest] = t;

    }
}

The desired output should be :

16 14 10 8 7 9 3 2 4 1

Whereas I am getting

4 3 16 14 8 9 10 2 1 7

Can someone please help as to why the heap is not being built properly ?

Thanks

  • 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-08T05:22:37+00:00Added an answer on June 8, 2026 at 5:22 am

    I ran your code, and in addition to what Jodaka said, I found one more error

    for (kk = 0; kk <= Arr.length - 1; kk++) {
            heapM(Arr, kk);
    }
    

    should be

    for (kk = Arr.length -1; kk >= 0; kk--) {
            heapM(Arr, kk);
    }
    

    because when doing MaxHepify, you start from the end, and go backwards.
    and

     int left = i * 2;
     int right = i * 2 + 1;
    

    should be, as Jodaka said

    int left = 2*i+1;
    int right = 2*i + 2;
    

    I ran it here and the output is now correct

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

Sidebar

Related Questions

I am trying to create a generic container (e.g. java code below), which has
I am trying to create a histogram data using following query: SELECT FLOOR(Max_Irrad/10) AS
I'm trying to create a benchmark test with java. Currently I have the following
Im trying to create a gallery for my website, so i use max-height/width to
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
I'm trying to create this slider http://jqueryui.com/demos/slider/#rangemax Is it possible to parametrize the max
I'm trying to create a custom route like: search/result/city/p1/state/p2/zip/p3/min/p4/max/p5/bed/p6/bath/p7/cats/p8/dogs/p9/parking/p10 Where search is the controller
I'm trying to create a program that: asks the user to input a max.
I'm trying to create a form. Here is my form class: class RegisterForm(forms.Form): login=forms.CharField(min_length=5,max_length=15)

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.