Skip to content

Commit

Permalink
Update smooth_sort.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
kanishkgits committed Oct 7, 2024
1 parent e691798 commit 78fbc3e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/sorting/smooth_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ fn remove_from_leonardo_heap(
}
}

fn heapify_leonardo(nums: &mut Vec<i32>, index: usize, sizes: &Vec<usize>, heaps: usize) {
fn heapify_leonardo(nums: &mut [i32], index: usize, sizes: &[usize], heaps: usize) {
let mut current = index;
let mut heap_size = sizes[heaps];

while heaps > 1 {
let left_child = current - heap_size;
let right_child = current - 1;

if heap_size > current {
break;
}

let left_child = current.checked_sub(heap_size).expect("Underflow error: heap_size is too large");
let right_child = current.checked_sub(1).expect("Underflow error: current is too small");

if nums[current] < nums[left_child] {
nums.swap(current, left_child);
current = left_child;
Expand All @@ -66,7 +70,8 @@ fn heapify_leonardo(nums: &mut Vec<i32>, index: usize, sizes: &Vec<usize>, heaps
} else {
break;
}


heaps -= 1;
heap_size -= 1;
}
}
Expand Down

0 comments on commit 78fbc3e

Please sign in to comment.