Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Implementing a heapSort().

Posted on 2006-11-28
3
Medium Priority
?
853 Views
Last Modified: 2013-11-23
I built the heap and it works fine but I can't seem to get the sort to work right. Any help on getting started would be appreciated. Here's what I have so far.

import java.util.*;

public class Heap{
    private ArrayList<Integer> al;
   
    public Heap(){
        al=new ArrayList<Integer>();
    }
   
    public Heap(ArrayList<Integer> al){
        this.al=new ArrayList<Integer>(al);
    }
   
    public void insert(Integer obj){
        al.add(obj);
        int child=this.size()-1;
        int parent=(child-1)/2;
        while(parent>=0 && al.get(parent).compareTo(al.get(child))>0){
            Integer temp=al.get(child);
            al.set(child, al.get(parent));
            al.set(parent, temp);
            child=parent;
            parent=(child-1)/2;  
        }
    }
   
    public boolean isEmpty(){
        return al.isEmpty();
    }
   
    public int size(){
        return al.size();
    }
   
    public Integer delete(){
        if(this.isEmpty())
            return null;
        if(this.size()==1)
            return al.remove(0);
        Integer min=al.get(0);
        al.set(0, al.remove(this.size()-1));
        int parent=0; //each parent may have 0, 1, 2 children
        while(true){
            int leftChild=(2*parent)+1;
            int rightChild=leftChild+1;
            if(leftChild>=this.size())
                break;
            int minChild=leftChild;
            if(rightChild<this.size() && al.get(leftChild).compareTo(al.get(rightChild))>0)
                minChild=rightChild;
            if(al.get(parent).compareTo(al.get(minChild))>0){
                Integer temp=al.get(parent);
                al.set(parent, al.get(minChild));
                al.set(minChild, temp);
                parent=minChild;
            }
            else
                break;
        }
        return min;
    }
   
    public void heapSort(){
    }
}
0
Comment
Question by:bigmdawg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 450 total points
ID: 18030490
A very well explaining website can be found here:

http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/heap/heapen.htm

On the bottom there is even an implementation in Java.

Mark
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 18031433
Naturally you don't need to implement Heap because java 1.5 already has one
java.util.PriorityQueue
0
 
LVL 35

Accepted Solution

by:
girionis earned 600 total points
ID: 18035174
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question