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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:39:25+00:00 2026-06-13T11:39:25+00:00

I am trying some heapsorting in java for fun. First, I create a max-heap.

  • 0

I am trying some heapsorting in java for fun.

First, I create a max-heap. Then take the first element to variable max, move the last item from unarranged array to the heap top, and then push down.

I tried 21 elements, it works fine 70% of time, I am wondering whether anyone find the problem.

Thanks.

public class HeapSort extends Sort{
    public int sort(int arr[]) {
        for (int i = 1; i < arr.length; i++) {
            // add to heap
            int p = i;
            int pp = p /2 ;
            while (p > 0 && arr[pp] < arr[p]) {
                swap(arr, p, pp);
                p = pp;
                pp = p / 2;
            }

        }   

        for (int i = arr.length - 1; i > 0; i--) {
            swap(arr, 0, i);
            int p = 0;
            int child1 = (p << 1) + 1;
            int child2 = (p << 1) + 2;

            while (child2 < i || child1 < i) {
                if (child1 >= i) {
                    if (arr[p] < arr[child2]) {
                        swap(arr, p, child2);
                        p = child2;
                    } else { 
                        break;
                    }
                } else if (child2 >= i) {
                    if (arr[p] < arr[child1]) {
                        swap(arr, p, child1);
                        p = child1;
                    } else {
                        break;
                    }
                } else {
                    int minIdx = arr[child1] < arr[child2] ? child1 : child2;
                    int maxIdx = child1 == minIdx ? child2 : child1;

                    if (arr[p] < arr[maxIdx]) {
                        swap(arr, p, maxIdx);
                        p = maxIdx;
                    } else {

                        break;

                    }
                }
                child1 = (p << 1) + 1;
                child2 = (p << 1) + 2;
            }

        }
        return 0;

    }
    void swap(int arr[], int idx1, int idx2) {
        int tmp = arr[idx1]; arr[idx1] = arr[idx2]; arr[idx2] = tmp;
    }
    public String toString() {
        return "HeapSort";
    }

}
  • 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-13T11:39:26+00:00Added an answer on June 13, 2026 at 11:39 am

    You’re not building the heap correctly in the first step of heap-sort. You need to subtract one before dividing by two on zero based arrays.

    public class HeapSort extends Sort{ 
       public int sort(int arr[]) { 
          for (int i = 1; i < arr.length; i++) { // add to heap 
             int p = i;
             // int pp = p /2 ;  <======= Problem!
             int pp = (p-1) / 2; 
             while (p > 0 && arr[pp] < arr[p]) { 
                swap(arr, p, pp); p = pp; 
                //  pp = p / 2; // <=====Problem!
                pp = (p-1) /2;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying some code around object equality in java. As I have read
I am new to javascript and I'm trying some different things. Take this following
I'm trying some Java recently and look for some review of my style. If
I am trying some AJAX calls for the first time. My site is hosted
I'm trying some db schema changes to my db, using the sqlalchemy table.create and
I'm trying some GUI development in Java and when I added the following code,
I have been trying some different java compilers over the weekend and decided to
I discovered Guice last week... I'm trying some easy tricks with it. However, I'm
I am trying some things out with manually drawing things with a Java Graphics2D
i am trying some java code like this class Test { public static void

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.