Unlike choice sort, heapsort makes not waste time with a linear-time scan of the unsorted region; rather, heap sort keeps the unsorted region in a heap data structure to more quickly find the largest component in each step. Heapsort can be thought of as an better choice sort: like choice sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest component from it and inserting it into the sorted region.

The algorithm then repeatedly swaps the first value of the list with the last value, decrease the range of values considered in the heap operation by one, and sifting the new first value into its position in the heap. This repeats until the range of considered values is one value in length.

```
/*
* Copyright (c) 2017 Kotlin Algorithm Club
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.algorithmexamples.sorts
import com.algorithmexamples.datastructures.PriorityQueue
/**
* Heap sort is a comparison based sorting technique based on Binary Heap data structure. It 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.
*/
@ComparisonSort
@UnstableSort
class HeapSort : AbstractSortStrategy() {
override fun <T : Comparable<T>> perform(arr: Array<T>) {
for (k in arr.size / 2 downTo 1) {
PriorityQueue.sink(arr as Array<T?>, k - 1, arr.size - 1, reverseOrder<T>())
}
for (k in arr.size downTo 1) {
arr.exch(0, k - 1)
PriorityQueue.sink(arr as Array<T?>, 0, k - 2, reverseOrder<T>())
}
}
}
```