Heapsort

Heapsort is an unstable comparison sort algorithm with exceptional performance. Heapsort uses the insertion method and performs at O(n log (n)) in the best, average, and worst case.


Table of contents

Performance

Code

public class HeapSort {
    public static void main(String[] args)
    {
        int[]    array  = {12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8};
        HeapSort sorter = new HeapSort();
        sorter.heapSort(array);
        System.out.println(java.util.Arrays.toString(array));
    }

    void heapSort(int[] array)
    {
        int size = array.length;

        // build heapSort (rearrange array)
        for (int i = size / 2 - 1; i >= 0; i--)
            heapify(array, size, i);

        // one by one extract an element from heapSort
        for (int i = size - 1; i >= 0; i--)
        {
            // move current root to end
            int temp = array[0];
            array[0] = array[i];
            array[i] = temp;

            // call max heapify on the reduced heapSort
            heapify(array, i, 0);
        }
    }

    // to heapify a subtree rooted with node i which is an index in array[]
    static void heapify(int[] array, int size, int i)
    {
        int max   = i; // initialize max as root
        int left  = 2 * i + 1;
        int right = 2 * i + 2;

        // if left child is larger than root
        if (left < size && array[left] > array[max])
            max = left;

        // if right child is larger than max
        if (right < size && array[right] > array[max])
            max = right;

        // if max is not root
        if (max != i)
        {
            // swap
            int temp = array[i];
            array[i] = array[max];
            array[max] = temp;

            // recursively heapify the affected sub-tree
            heapify(array, size, max);
        }
    }
}
public class HeapSortGeneric<T extends Comparable<? super T>> {
    public static void main(String[] args)
    {
        // example using Strings
        String[]                 arrayOfStrings = {"Andree", "Leana", "Faviola", "Loyce", "Quincy", "Milo", "Jamila", "Toccara", "Nelda", "Blair", "Ernestine", "Chara", "Kareen", "Monty", "Rene", "Cami", "Winifred", "Tara", "Demetrice", "Azucena"};
        HeapSortGeneric<String> stringSorter   = new HeapSortGeneric<>();
        stringSorter.heapSort(arrayOfStrings);
        System.out.println(java.util.Arrays.toString(arrayOfStrings));

        // example using Doubles
        Double[]                 arrayOfDoubles = {0.35, 0.02, 0.36, 0.82, 0.27, 0.49, 0.41, 0.17, 0.30, 0.89, 0.37, 0.66, 0.82, 0.17, 0.20, 0.96, 0.18, 0.25, 0.37, 0.52};
        HeapSortGeneric<Double> doubleSorter   = new HeapSortGeneric<>();
        doubleSorter.heapSort(arrayOfDoubles);
        System.out.println(java.util.Arrays.toString(arrayOfDoubles));
    }

    void heapSort(T[] array)
    {
        int size = array.length;

        // build heapSort (rearrange array)
        for (int i = size / 2 - 1; i >= 0; i--)
            heapify(array, size, i);

        // one by one extract an element from heapSort
        for (int i = size - 1; i >= 0; i--)
        {
            // move current root to end
            T temp = array[0];
            array[0] = array[i];
            array[i] = temp;

            // call max heapify on the reduced heapSort
            heapify(array, i, 0);
        }
    }

    // to heapify a subtree rooted with node i which is an index in array[]
    void heapify(T[] array, int size, int i)
    {
        int max   = i; // initialize max as root
        int left  = 2 * i + 1;
        int right = 2 * i + 2;

        // if left child is larger than root
        if (left < size && array[left].compareTo(array[max]) > 0)
            max = left;

        // if right child is larger than max
        if (right < size && array[right].compareTo(array[max]) > 0)
            max = right;

        // if max is not root
        if (max != i)
        {
            // swap
            T temp = array[i];
            array[i] = array[max];
            array[max] = temp;

            // recursively heapify the affected sub-tree
            heapify(array, size, max);
        }
    }
}
#include <iostream>
#include <vector>

// to heapify a subtree rooted with node i which is an index in array[]
void heapify(std::vector<int> &arr, int size, int i) {
    int max = i; // initialize max as root
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    // if left child is larger than root
    if (left < size && arr[left] > arr[max])
        max = left;

    // if right child is larger than max
    if (right < size && arr[right] > arr[max])
        max = right;

    // if max is not root
    if (max != i) {
        // swap
        int temp = arr[i];
        arr[i] = arr[max];
        arr[max] = temp;

        // recursively heapify the affected sub-tree
        heapify(arr, size, max);
    }
}

void heapSort(std::vector<int> &arr) {
    int size = arr.size();

    // build heapSort (rearrange array)
    for (int i = size / 2 - 1; i >= 0; i--) {
        heapify(arr, size, i);
    }

    // one by one extract an element from heapSort
    for (int i = size - 1; i >= 0; i--) {
        // move current root to end
        int temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;

        // call max heapify on the reduced heapSort
        heapify(arr, i, 0);
    }
}

int main() {
    std::vector<int> arr = {12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8};
    heapSort(arr);
    for (int i; i < arr.size(); i++) {
        std::cout << arr[i];
        if (i < arr.size() - 1) std::cout << ", ";
    }
}
function heapSort(array) {
  let size = array.length

  // build heapSort (rearrange array)
  for (let i = Math.floor(size / 2 - 1); i >= 0; i--)
    heapify(array, size, i)

  // one by one extract an element from heapSort
  for (let i = size - 1; i >= 0; i--) {
    // move current root to end
    let temp = array[0]
    array[0] = array[i]
    array[i] = temp

    // call max heapify on the reduced heapSort
    heapify(array, i, 0)
  }
}

// to heapify a subtree rooted with node i which is an index in array[]
function heapify(array, size, i) {
  let max = i // initialize max as root
  let left = 2 * i + 1
  let right = 2 * i + 2

  // if left child is larger than root
  if (left < size && array[left] > array[max])
    max = left

  // if right child is larger than max
  if (right < size && array[right] > array[max])
    max = right

  // if max is not root
  if (max != i) {
    // swap
    let temp = array[i]
    array[i] = array[max]
    array[max] = temp

    // recursively heapify the affected sub-tree
    heapify(array, size, max)
  }
}

let array = [12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8]
heapSort(array)
alert(array)

Discussion