Simple program for Heap Sort in Java

Arrange the following inputs using heap sort.

public class SortingAlgorithm {
    public static void main(String[] args) {
        int[] array = { 12, 11, 13, 5, 6, 7};
        heapSort(array);
        printArray(array);
    }

    public static void heapSort(int[] array){
        int len = array.length;
        for (int i = len/2 -1; i >= 0; i--){
            heapify(array, len, i);
        }

        for (int i = len - 1; i > 0; i--){
            int temp = array[0];
            array[0] = array[i];
            array[i] = temp;

            heapify(array, i, 0);
        }
    }

    public static void heapify(int[] array, int n, int i){
        int largest = i;
        int left = 2 * i + 1;
        int right = 2 * i + 2;

        if (left < n && array[left] > array[largest]){
            largest = left;
        }

        if (right < n && array[right] > array[largest]){
            largest = right;
        }

        if (largest != i){
            int swap = array[i];
            array[i] = array[largest];
            array[largest] = swap;

            heapify(array, n, largest);
        }
    }

    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top