Heap Sort Algorithm

Heap Sort Algorithm is a comparison-based sorting technique that utilizes the binary heap data structure to sort a sequence of elements in either ascending or descending order. The main idea behind the algorithm is to build a binary heap (either max-heap or min-heap) from the given input data, then repeatedly extract the root element and insert it into the sorted section of the array while maintaining the heap property. In the first step, the algorithm builds a max-heap for sorting in ascending order (or a min-heap for sorting in descending order) using the input data. This process involves iteratively comparing parent and child nodes and swapping them if they do not follow the heap property. Once the heap is constructed, the algorithm then repeatedly extracts the root element (the largest or smallest element in the heap) and swaps it with the last element of the unsorted part of the array until the entire array is sorted. After each extraction, the heap is restructured to maintain its properties. This process continues until the entire array is sorted, resulting in an efficient and in-place sorting algorithm with a time complexity of O(n log n) for both the best and worst cases.
package sort

/**
 * This function implements the Heap Sort.
 *
 * @param array The array to be sorted
 * Sorts the array in increasing order
 *
 * Worst-case performance       O(n*log(n))
 * Best-case performance        O(n*log(n))
 * Average-case performance     O(n*log(n))
 * Worst-case space complexity  O(1) (auxiliary)
 */
fun <T: Comparable<T>> heapSort(array: Array<T>) {
    buildMaxHeap(array)
    transformMaxHeapToSortedArray(array)
}

/**
 * This function changes the element order of the array to represent a max
 * binary tree.
 *
 * @param array The array containing the elements
 * @param index Index of the currently largest element
 */
fun <T: Comparable<T>> maxheapify(array: Array<T>, heapSize: Int, index: Int) {
    val left = 2 * index + 1
    val right = 2 * index + 2
    var largest = index

    if(left < heapSize && array[left] > array[largest])
        largest = left
    if(right < heapSize && array[right] > array[largest])
        largest = right
    if(largest != index) {
        swapElements(array, index, largest)
        maxheapify(array, heapSize, largest)
    }
}

/**
 * Arrange the elements of the array to represent a max heap.
 *
 * @param array The array containing the elements
 */
private fun <T: Comparable<T>> buildMaxHeap(array: Array<T>) {
    val n = array.size
    for(i in (n / 2 - 1) downTo 0)
        maxheapify(array, n, i)
}

/**
 * Arrange the elements of the array, which should be in order to represent a
 * max heap, into ascending order.
 *
 * @param array The array containing the elements (max heap representation)
 */
private fun <T: Comparable<T>> transformMaxHeapToSortedArray(array: Array<T>) {
    for(i in (array.size - 1) downTo 0) {
        swapElements(array, i, 0)
        maxheapify(array, i, 0)
    }
}

LANGUAGE:

DARK MODE: