From 4caf6e5d3215104aadbf59f86eb96ea1b6a722a9 Mon Sep 17 00:00:00 2001 From: "gongruihan.1" Date: Mon, 27 Apr 2026 16:14:35 +0800 Subject: [PATCH] fix: off-by-one bounds check in heap_sort::sink Change j < n to j + 1 < n to prevent out-of-bounds access on arr[j + 1] when j == n - 1. Fixes #3 --- algorithmica/src/sort/heap_sort.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithmica/src/sort/heap_sort.rs b/algorithmica/src/sort/heap_sort.rs index f7a0483..6433a85 100644 --- a/algorithmica/src/sort/heap_sort.rs +++ b/algorithmica/src/sort/heap_sort.rs @@ -39,7 +39,7 @@ pub fn sink bool>(arr: &mut [T], k: usize, n: usize, p: let mut k = k; while 2 * k < n { let mut j = 2 * k + 1; - if j < n && p(&arr[j], &arr[j + 1]) { + if j + 1 < n && p(&arr[j], &arr[j + 1]) { j += 1; } if !p(&arr[j], &arr[k]) {