Problem with QuickSort implementation


I create my own QuickSort implementation but sometimes when i start code i have problem with index.
I have 200 size vector but my code show very big index and program panic, and i dont know how to solve it.

Its link to my code.

Casting the i32 value of -1 to an usize results in 18446744073709551615. Is that by any chance the number you got? You will have to check whether the pivot - 1 or pivot + 1 operations end up outside the array and handle that properly

Some general advice:

  1. There is already a swap method on Vec.
  2. It doesn't make sense to cast to an i32 here. Keep all the integers as usize instead.
  3. You can remove the return in partition. (using clippy would have caught this)

Thanks for help ;D Everything work corectly now

This part might cause issues if the pivot is a value larger than isize can carry.

  if pivot as isize - 1 < 0 || pivot + 1 > arr.len(){

I believe the following might do what you want without the cast to isize.

    if pivot < 1 || pivot + 1 > arr.len(){

You are right, i change this, and i had to divide this if to one case if and call the recursion.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.