# 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) && (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
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p heap_sort(list)
end
Given an unsorted array of n elements, write a function to sort the array
O(n log n) Worst case performance
O(n log n) (distinct keys)
or O(n) (equal keys) Best-case performance
O(n log n) Average performance
O(1) Worst case auxiliary
Input data: 4, 10, 3, 5, 1
4(0)
/ \
10(1) 3(2)
/ \
5(3) 1(4)
The numbers in bracket represent the indices in the array
representation of data.
Applying heapify procedure to index 1:
4(0)
/ \
10(1) 3(2)
/ \
5(3) 1(4)
Applying heapify procedure to index 0:
10(0)
/ \
5(1) 3(2)
/ \
4(3) 1(4)
The heapify procedure calls itself recursively to build heap
in top down manner.
