El código realiza un Ordenamiento de datos numéricos haciendo uso del Método Heap Sort en Ruby:
def heap(arr, n, i)
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n && arr[left] > arr[largest]
largest = left
end
if right < n && arr[right] > arr[largest]
largest = right
end
if largest != i
arr[i], arr[largest] = arr[largest], arr[i]
heap(arr, n, largest)
end
end
def ordenamiento_heap_sort(arr)
n = arr.length
(n / 2 - 1).downto(0) do |i|
heap(arr, n, i)
end
(n - 1).downto(1) do |i|
arr[0], arr[i] = arr[i], arr[0]
heap(arr, i, 0)
end
end
arr = [64, 34, 25, 12, 22, 11, 90]
ordenamiento_heap_sort(arr)
puts "Arreglo ordenado: #{arr}"