-
Notifications
You must be signed in to change notification settings - Fork 267
/
heap_sort.rb
49 lines (40 loc) · 872 Bytes
/
heap_sort.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true
# Sort an array using the HeapSort algorithm
class Heapsort
attr_reader :array_sorted
def initialize
@array_sorted = []
end
def init(array)
heap_sort(array)
end
private
def heap_sort(array)
n = array.length
a = [nil] + array
(n / 2).downto(1) do |i|
heapify(a, i, n)
end
while n > 1
a[1], a[n] = a[n], a[1]
n -= 1
heapify(a, 1, n)
end
a.drop(1)
@array_sorted = a
end
def heapify(array, parent, limit)
root = array[parent]
while (child = 2 * parent) <= limit
child += 1 if child < limit && array[child] < array[child + 1]
break if root >= array[child]
array[parent] = array[child]
parent = child
array[parent] = root
end
end
end
# test
h_s = Heapsort.new
h_s.init([1, 4, 10, 2, 3, 32, 0])
p h_s.array_sorted