Heap Sort

Heap Structure

Heap.java

public class Heap { 
 
    int[] heap; 
    int numberOfNodes = 0; 
 
    public Heap(int data, int size) { 
        heap = new int[15]; 
        heap[0] = data; 
        numberOfNodes = 1; 
    } 
 
    void addElement(int data) { 
        System.out.println("Adding " + data + "..."); 
        System.out.println(); 
        heap[numberOfNodes] = data; 
        int currentIndex = numberOfNodes; 
 
        // loop terminates when no swap occurs 
        while (currentIndex > 0) { 
            int newIndex = compareWithParent(currentIndex); 
            if (newIndex == currentIndex) { 
                break; 
            } 
            currentIndex = newIndex; 
        } 
 
        numberOfNodes++; 
 
        printHeap(); 
    } 
 
    void removeElement() { 
 
        System.out.println("Removing root..."); 
        System.out.println(); 
 
        if (numberOfNodes > 0) { 
 
            heap[0] = heap[numberOfNodes - 1]; 
            numberOfNodes--; 
 
            sortIterate(); 
        } 
 
        printHeap(); 
    } 
 
    void sort() { 
 
        if (numberOfNodes > 0) { 
            int iterations = numberOfNodes; 
            for (int i = 0; i < iterations; i++) { 
                System.out.println("Sorting..."); 
                System.out.println(); 
                swap(0, numberOfNodes - 1); 
                numberOfNodes--; 
                sortIterate(); 
 
                printHeap(); 
            } 
            numberOfNodes = iterations; 
        } 
 
        System.out.println("Sorted heap:"); 
        System.out.println(); 
        printHeap(); 
    } 
 
    void sortIterate() { 
 
        int currentNode = 0; 
 
        boolean stop = false; 
 
        while (!stop) { 
 
            stop = true; 
 
            if (rightChild_index(currentNode) < numberOfNodes) { 
                if (heap[rightChild_index(currentNode)] > heap[leftChild_index(currentNode)] 
                        && heap[rightChild_index(currentNode)] > heap[currentNode]) { 
                    swap(rightChild_index(currentNode), currentNode); 
                    currentNode = rightChild_index(currentNode); 
                    stop = false; 
                } 
                else if (heap[leftChild_index(currentNode)] > heap[rightChild_index(currentNode)] 
                        && heap[leftChild_index(currentNode)] > heap[currentNode]) { 
                    swap(leftChild_index(currentNode), currentNode); 
                    currentNode = leftChild_index(currentNode); 
                    stop = false; 
                } 
            } 
 
            else if (leftChild_index(currentNode) < numberOfNodes) { 
                if (heap[leftChild_index(currentNode)] > heap[currentNode]) { 
                    swap(leftChild_index(currentNode), currentNode); 
                    currentNode = leftChild_index(currentNode); 
                    stop = false; 
                } 
            } 
        } 
 
    } 
 
    int compareWithParent(int index) { 
 
        // return value of parent if swap occurs 
        // else return original index value 
 
        if (heap[index] > heap[parent_index(index)]) { 
            swap(index, parent_index(index)); 
            return parent_index(index); 
        } 
        return index; 
    } 
 
    void swap(int index1, int index2) { 
        int temp = heap[index1]; 
        heap[index1] = heap[index2]; 
        heap[index2] = temp; 
    } 
 
    int parent_index(int index) { 
        return (index - 1) / 2; 
    } 
 
    int leftChild_index(int index) { 
        return index * 2 + 1; 
    } 
 
    int rightChild_index(int index) { 
        return index * 2 + 2; 
    } 
 
    void printHeap() { 
        printTreeStructure(); 
        System.out.println(); 
        printArrayStructure(); 
        System.out.println(); 
    } 
 
    void printTreeStructure() { 
        System.out.println("Tree representation:"); 
        breadthFirstIterate(0, 0); 
    } 
 
    void breadthFirstIterate(int index, int depth) { 
 
        for (int i = 0; i < depth; i++) { 
            System.out.print("---"); 
        } 
        System.out.println(heap[index]); 
 
        depth++; 
        if (rightChild_index(index) < numberOfNodes) { 
            breadthFirstIterate(leftChild_index(index), depth); 
            breadthFirstIterate(rightChild_index(index), depth); 
        } 
        else if (leftChild_index(index) < numberOfNodes) { 
            breadthFirstIterate(leftChild_index(index), depth); 
        } 
    } 
 
    void printArrayStructure() { 
        System.out.println("Array representation:"); 
        for (int i = 0; i < numberOfNodes; i++) { 
            System.out.print(heap[i] + " "); 
        } 
        System.out.println(); 
    } 
 
}

Implementation of Heap Sort

ImplementHeap.java

public class ImplementHeap { 
 
    public static void main(String[] args) { 
 
        System.out.println("Initializing Heap with " + 8); 
        System.out.println(); 
        Heap heap = new Heap(8, 15); 
        heap.printHeap(); 
 
        heap.addElement(10); 
        heap.addElement(2); 
        heap.addElement(6); 
        heap.addElement(12); 
        heap.addElement(9); 
 
        heap.removeElement(); 
 
        heap.addElement(28); 
        heap.addElement(15); 
        heap.addElement(5); 
        heap.addElement(7); 
        heap.addElement(1); 
 
        heap.sort(); 
 
    } 
 
}