I have a quicksort algorithm that works individually, but when I pass it into a function that runs the algorithm a lot of times (1 million times to be specific), I encounter that error:
thread 'main' panicked at 'attempt to subtract with overflow', src\quicksort.rs:17:30 note: run with
RUST_BACKTRACE=1
environment variable to display a backtrace
quicksort.rs:
fn partition(arr: &mut Vec<isize>, low: usize, high: usize) -> usize {
let mut i: usize = low;
let pivot: isize = arr[high];
for j in low..high {
if arr[j] <= pivot {
arr.swap(i, j);
i += 1;
}
}
arr.swap(i, high);
return i;
}
pub fn quick_sort(arr: &mut Vec<isize>, low: usize, high: usize) {
if low < high {
let pivot_index: usize = partition(arr, low, high);
quick_sort(arr, low, pivot_index - 1);
quick_sort(arr, pivot_index + 1, high);
}
}
What could be the reason for the overflow?