Search code examples
javaalgorithmlistsortingheapsort

(Java) Heapsort - Implementation not sorting half elements?


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 ;)


Solution

  • 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.