Skip to content

Commit e7a9d74

Browse files
committed
Add a binary_heap module
binary_heap.sort(array,cmp) implements a heap sort of a list in Berry, with respect to a comparator. binary_heap.make_heap(array,cmp) transforms a Berry list into a heap. binary_heap.remove_heap(array,cmp) removes and returns the first element of a binary heap and maintains the heap property for the remaining elements. By repeatedly invoking it, one can obtain the first few elements of a Berry list in a sorted order, without having to sort all elements. The example program can be run as follows: berry -m modules modules/examples/binary_heap_sort.be
1 parent 678a241 commit e7a9d74

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

modules/binary_heap.be

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# https://en.wikipedia.org/wiki/Binary_heap
2+
# similar to C++11 std::make_heap
3+
4+
def heapify(array, cmp, i)
5+
while true
6+
var m = i, child = 2 * i
7+
child += 1
8+
if child >= array.size() return end
9+
if cmp(array[child], array[m]) m = child end
10+
child += 1
11+
if child < array.size() && cmp(array[child], array[m]) m = child end
12+
if m == i break end
13+
var e = array[i]
14+
array[i] = array[m]
15+
i = m
16+
array[i] = e
17+
end
18+
end
19+
20+
var binary_heap = module("binary_heap")
21+
22+
binary_heap.make_heap = def(array, cmp)
23+
for i: range(size(array) / 2, 0, -1) heapify(array, cmp, i) end
24+
end
25+
26+
binary_heap.remove_heap = def(array, cmp)
27+
if array.size() == 0
28+
return array
29+
elif array.size() == 1
30+
return array.pop()
31+
end
32+
var m = array[0]
33+
array[0] = array.pop()
34+
heapify(array, cmp, 0)
35+
return m
36+
end
37+
38+
binary_heap.sort = def(array, cmp)
39+
var heap = array.copy()
40+
binary_heap.make_heap(heap, cmp)
41+
array.clear()
42+
for i: 1..heap.size() array.push(binary_heap.remove_heap(heap, cmp)) end
43+
end
44+
45+
return binary_heap

modules/examples/binary_heap_sort.be

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import binary_heap
2+
3+
var pi = [3,14,15,92,65,35,89,79,32,38,46,26,43,38,32,79,50,28,84,19,71,69,39]
4+
# Display the elements of pi[] in ascending order
5+
var h = pi.copy()
6+
var less = / a b -> a < b, sort = binary_heap.sort
7+
sort(h, less)
8+
print(h)
9+
10+
# Display the 5 largest elements of pi[]
11+
var cmp = / a b -> a > b
12+
var max = []
13+
binary_heap.make_heap(h, cmp)
14+
for i: 1..5 max.push(binary_heap.remove_heap(h, cmp)) end
15+
print(max)
16+
17+
# Display the 7 largest elements of pi[] in ascending order of index
18+
h = []
19+
for i: 0..pi.size()-1 h.push(i) end
20+
cmp = / a b -> pi[a] > pi[b]
21+
binary_heap.make_heap(h, cmp)
22+
max = []
23+
for i: 1..7 max.push(binary_heap.remove_heap(h, cmp)) end
24+
sort(max, less)
25+
for i: 0..6 print(f"{max[i]}: {pi[max[i]]}") end

0 commit comments

Comments
 (0)