1
0
mirror of https://github.com/golang/go synced 2024-11-24 22:47:58 -07:00

Update heap.go

This commit is contained in:
Lorenzo Tinfena 2022-11-18 01:04:47 +01:00
parent 275cad79a4
commit 0055dd6ff9

View File

@ -89,7 +89,7 @@ func Fix(h Interface, i int) {
func up(h Interface, j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !h.Less(j, i) {
if i == -1 || h.Less(i, j) {
break
}
h.Swap(i, j)
@ -104,17 +104,17 @@ func down(h Interface, i0, n int) bool {
if j <= 0 { // right child <= 0 after int overflow (if true, it breaks also if the left child has index = maxint, because is impossible to has n > maxint)
break
}
if j < n { // check the bounds firstly for the right child because in most cases that true (and implicitily left child if less than n)
if j < n { // check the bounds firstly for the right child because in most cases that's true (and implicitily left child if less than n)
if h.Less(j-1, j) {
j--
}
} else {
j--
if j >= n {
if j <= n && h.Less(j-1, i) { // if true => j-1 == n
h.Swap(i, j-1)
}
break
}
}
if !h.Less(j, i) {
if h.Less(i, j) {
break
}
h.Swap(i, j)