forked from AdaGold/heaps
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathheap_sort.rb
More file actions
61 lines (48 loc) · 1.14 KB
/
heap_sort.rb
File metadata and controls
61 lines (48 loc) · 1.14 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
require "pry"
# This method uses a heap to sort an array.
# Time Complexity: O (n log n) where n is the number of elements in the heap
# Space Complexity: O (1)
# Adapted from diagrams and pseudocode in https://medium.com/basecs/heapify-all-the-things-with-heap-sort-55ee1c93af82
def heapsort(list)
limit = list.length
build_max_heap(list)
while limit > 0 # O(n)
swap(list, 0, limit - 1)
limit -= 1
heapify(list, 0, limit) # O(log n)
end
return list
end
def build_max_heap(list)
index = list.length/2 - 1
while index >= 0
heapify(list, index, list.length)
index -= 1
end
end
def heapify(list, index, limit)
root = index
while index < limit
left = (2 * index) + 1
right = (2 * index) + 2
if left >= limit && right >= limit
return
elsif right >= limit
max = left
else
max = [left, right].max_by {|x| list[x]}
end
if list[max] > list[root]
root = max
else
return
end
swap(list, root, index)
index = root
end
end
def swap(list, index_1, index_2)
temp = list[index_1]
list[index_1] = list[index_2]
list[index_2] = temp
end