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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:34:10+00:00 2026-06-13T20:34:10+00:00

I’ve wrote two different implementations of heapsort today and both have given me the

  • 0

I’ve wrote two different implementations of heapsort today and both have given me the same results:

Object i: 18
Object i: 11
Object i: 10
Object i: 9
Object i: 8
Object i: 3
Object i: 7
Object i: 1
Object i: 4

Now I’ve checked my code with this page here; and believe one of my implementations is exactly the same as the pseudo-code would suggest, whilst the other is very similar to one of the Java implementations.

I’d like to stress the fact that I’ve actually wrote two different versions, and checked them against implementations I can find! So I am really stumped at the moment! I’ve stepped through it with a debugger a few times – but have probably must’ve something along the way? I even made a debugging function which just looped through the list and outputted the contents using System.out.println() – but this still didn’t help a great deal!

The algorithm is working on a List – and I have done nothing to optimise it at this stage; it merely is a bit of an experiment at the moment. I have working implementations of QuickSort, BubbleSort & Insertion Sort – but this one has left me stumped!

My first implementation is below:

public static List<Integer> execSort(List<Integer> s) {

    int n = (s.size()-1);
    Integer t;

    for(int i = n/2; i>0; i--){
        s = downheap(s, i, n);
    }

    while(n >= 1){
        t= s.remove(0);
        s.add(0, s.remove(n-1));
        s.add(n-1, t);

        n--;
        s = downheap(s, 1, n);
    } 

    return s;
}


public static List<Integer> downheap(List<Integer> s, int i, int n){
    Integer t = s.get(i-1);
    int j;

    while( i <= n/2 ){  
        j = i*2;

        if( (j<n) && (s.get(j-1) < s.get(j)))
            j++;

        if( t >= s.get(j-1)){
            break;
        } else {
            /* Swap them, without using a third variable 
                        - although with all the get()/set() methods
                        it would be better to have a third one, doh! */ 
            s.set(i-1, (s.get(i-1) + s.get(j-1)));
            s.set(j-1, (s.get(i-1) - s.get(j-1)));
            s.set(i-1, (s.get(i-1) - s.get(j-1)));

            i=j;
        }
    }

    s.set(i-1, t);
    return s;
}

You can also see them on Github as Gists:
– Implementation 1
– Implementation 2

Any ideas as to why some of the elements don’t want to sort?! I’m aware that this implementation is going to be sub-optimal, that working on a List<> isn’t going to be the best data structure and I should probably look at using primitive data-types as opposed to (ab)using auto-boxing… but that’s for another post! I just want a working version before I try and improve it 😉

  • 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-13T20:34:11+00:00Added an answer on June 13, 2026 at 8:34 pm

    In the gist (you accidentally linked both to the same), you have a few typos

    public static List<Integer> execSort(List<Integer> s) {
    
        int start = (s.size()/2)-1;
        int end = s.size()-1;
    
        while( start >= 0){
            s = sift(s, start, end);
    

    sift takes the count as last argument, not the last index, so the argument ought to be s.size() (or end+1) instead of end.

    public static List<Integer> sift(List<Integer> s, int start, int count){
    
        int root = start;
    
        while( ((root*2)+1) < 2 ){
    

    That must be while(root*2+1 < count), and not < 2.

    In the code you have here, you have in part the same problem (caused by an odd indexing strategy, I suspect):

        for(int i = n/2; i>0; i--){
            s = downheap(s, i, n);
    

    Since you always get(i-1) resp. j-1 in downheap, you need an upper bound of s.size() or n+1 while building the initial heap.

        }
    
        while(n >= 1){
    

    This loop should only run while n > 1, or you’ll swap the smallest element out of place.

            t= s.remove(0);
            s.add(0, s.remove(n-1));
            s.add(n-1, t);
    

    The old root must go in the last place, that’s place n, not n-1, s.add(n,t).

            n--;
            s = downheap(s, 1, n);
        } 
    

    In downheap, the final

        s.set(i-1, t);
    

    is superfluous, you always swap t, so when that line is reached, the element at i-1 already is t.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've tracked down a weird MySQL problem to the two different ways I was
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.