quickselect icon indicating copy to clipboard operation
quickselect copied to clipboard

Algorithm yields incorrect results

Open maja42 opened this issue 5 years ago • 1 comments

The following input data:

arr := []int{65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39}
pivot := 8
_ = quickselect.IntQuickSelect(arr, pivot)

reorders the data to the following:

[28 33 21 22 50 12 28 39 *59* 56 90 53 65 77 95]

As you can see, the value at index 8 (59) is higher than the 56 at index 9.

A correct example would be:

[21 12 22 33 28 28 39 50 53 56 59 65 90 77 95]

(as produces by https://github.com/keegancsmith/nth)

maja42 avatar May 21 '20 15:05 maja42

This isn't a bug. Quickselect just selects 8 smallest elements and it does not guarantee anything about element at index 8.

pracj3am avatar Apr 28 '21 08:04 pracj3am