Skip to content

Commit ede895a

Browse files
authored
fix: make heap_sort work for empty input (#200)
1 parent 559193a commit ede895a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/sorts/heap_sort.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ After the largest element has been extracted, the tree is updated to maintain th
2828
Contributed By:- [Frank Schmitt](https://github.com/frankschmitt)
2929
"""
3030
function heap_sort!(arr::Vector{T}, gt = >, N::Int = length(arr)) where {T}
31+
if isempty(arr)
32+
return
33+
end
3134
n = N
3235
i = div(n, 2)
3336
t = -1

test/sorts.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ using TheAlgorithms.Sorts
2828
y = [5, 2, 2, 2, 1, 5, 2, 34, 6, 4, 3, 2, 1, 2, 3, 4, 10, 1]
2929
f(y)
3030
@test y == [1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 10, 34]
31+
32+
z = []
33+
f(z)
34+
@test z == []
35+
36+
s = [1]
37+
f(s)
38+
@test s == [1]
3139
end
3240
end
3341
end

0 commit comments

Comments
 (0)