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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:14:09+00:00 2026-06-15T11:14:09+00:00

was wondering if i could get some quick help with a heapsort implementation. I

  • 0

was wondering if i could get some quick help with a heapsort implementation. I have it working and sorting fine but in the output it is always everything is sorted except the first number. It’s probably just a check somewhere but i have gone over my code and tried changing values but nothing produced the results i needed. Any advice to where i went wrong?
here is my source code:

code removed, problem was solved!

thanks guys!

  • 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-15T11:14:10+00:00Added an answer on June 15, 2026 at 11:14 am
    private static void movedown(double [] a, int k, int c) {
        while (2*k <= c-1) {
            int j = 2*k+1;
            if (j <= c-1 && less(a[j], a[j+1])) j++;
            if (!less(a[k], a[j])) break;
            exch(a, k, j);
            k = j;
        }
    }
    
    public static void heapsort(double [] a, int count) {
        for (int k = count/2; k >= 0; k--)
            movedown(a, k, count);
        while (count >= 1) {
            exch(a, 0, count--);
            movedown(a, 0, count);
        }
    }
    

    I have fixed your bug and tested it on my machine. It should work. Just a couple minor changes in these two methods.

    To summarize what you didn’t get right:

    1. In heapsort method, the count you passed in is zero-based index. However, when you built the heap you only looped to k = 1, i.e., one more iteration to go.

    2. In movedown method, you should have known the left child index is 2*k+1 while the right child index is 2*k+2.

    That you didn’t keep consistent with your indexing choices(i.e., 0-based vs. 1-based) resulted in the bug I guess.

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

Sidebar

Related Questions

I was wondering if i could get some help. What i am trying to
I was wondering if I could get some help with the code I pasted
I was wondering if I could get some refactoring help to make this query
I was wondering if i could get some help with filtering a select list
I was wondering if i could get some help to prevent a duplicate entry
i was wondering if i could get some help for my code. I put
I was wondering if I could get some help on a proper way of
net and wondering if I could get some get help with this problem. The
Wondering if I could get some advice and direction on this following requirement: Need
I am a new iphone developer and was wondering if I could get some

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.