Monday, October 17, 2016

Heap Sort implementation in Java

Heap Sort:

Heap sort is a comparison based sorting technique based on Binary Heap Data-Structures.Now, What is Binary Heap?

Binary Heap is a complete Binary Tree in which every level is completely filled except possibly the last.So,A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a parent node is greater(or smaller) than the values in its two children nodes. The former is called as max heap and the latter is called min heap. The heap can be represented by binary tree or array.

So Heap sort is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element.

Now, again question comes why heap sort can be represented by binary tree or array?

Ans: Because, we all know,a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based representation is space efficient.

If the parent node is stored at index I, the left child can be calculated by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).

Heap Sort Algorithm for Sorting in Increasing Order:

1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of tree.
3. Repeat above steps until size of heap is greater than 1.


Sample Code:

/**
 * @author Abhinaw.Tripathi
 *
 */
public class HeapSortApp
{
public void sort(int arr[])
    {
        int n = arr.length;

        for (int i = n / 2 - 1; i >= 0; i--)
            heapify(arr, n, i);

        for (int i=n-1; i>=0; i--)
        {          
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            heapify(arr, i, 0);
        }
    }

   
    void heapify(int arr[], int n, int i)
    {
        int largest = i;  // Initialize largest as root
        int l = 2*i + 1;  // left = 2*i + 1
        int r = 2*i + 2;  // right = 2*i + 2

        // If left child is larger than root
        if (l < n && arr[l] > arr[largest])
            largest = l;

        // If right child is larger than largest so far
        if (r < n && arr[r] > arr[largest])
            largest = r;

        // If largest is not root
        if (largest != i)
        {
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;
     
            heapify(arr, n, largest);
        }
    }

    /* A utility function to print array of size n */
    static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i]+" ");
        System.out.println();
    }

public static void main(String[] args)
{
int arr[] = {12, 11, 13, 5, 6, 7};

        HeapSortApp ob = new HeapSortApp();
        ob.sort(arr);

        System.out.println("Sorted array is-->");
        printArray(arr);

}
}

Output: 

Sorted array is-->

5 6 7 11 12 13








No comments: