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.

```
"""
Algorithm: Heap-Sort
Time-Complexity: O(nlogn)
"""
def heap_sort(array)
array_size = array.size
adjusted_array = [nil] + array
(array_size / 2).downto(1) do |i|
adjusted_down(adjusted_array, i, array_size)
end
while array_size > 1
adjusted_array[1], adjusted_array[array_size] = adjusted_array[array_size], adjusted_array[1]
array_size -= 1
adjusted_down(adjusted_array, 1, array_size)
end
adjusted_array.drop(1)
end
#Method to adjust heap in downward manner
def adjusted_down(adjusted_array, parent, limit)
top = adjusted_array[parent]
while (child = 2 * parent) <= limit
child += 1 if child < limit and adjusted_array[child] < adjusted_array[child + 1]
break if top >= adjusted_array[child]
adjusted_array[parent] = adjusted_array[child]
parent = child
end
adjusted_array[parent] = top
end
#Code for testing heapsort
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].shuffle
print heap_sort(array)
```