I am trying to implement the quicksort algorithm with rust, the problem is that, I have a var named 'i' which is used as an iterator, but at first, its value is '-1', and I cannot set it to a usize type because it is negative, but I also cannot set it to a isize type because isize types cannot be used as indices.
Partition function:
fn partition(arr: &mut [isize], low: usize, high: usize) -> usize {
let mut i: usize = low - 1; // when changed to isize, I do not encounter any errors but the algorithm itself doesnt work like it should.//
let mut j: usize = low;
let pivot: isize = arr[high];
while j < high {
if arr[j] <= pivot {
i += 1;
arr.swap(i, j);
}
j += 1;
}
arr.swap(i + 1, high);
return i + 1;
}
if I try to run the code above with another quicksort function, this is the error I get:
thread 'main' panicked at 'attempt to subtract with overflow', src\functions.rs:2:24
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace