algorithms
algorithms copied to clipboard
RSelect method not working as expected
Problem
The RSelect method is not returning the correct k-smallest element.
Here is an example:
arr := []int{5, 2, 6, 8, 3, 1}
x := RSelect(arr, 6, 0)
fmt.Println("Value: ", x)
The printed value is 5
, when it should be 1
.
Test
The test implemented for RSelect is running successfully, showing that it was implemented incorrectly.
There is an error in the applied boolean logic. The test is using &&
to validate the returned values, when it should be using ||
.
Here is the correct test implementation:
func TestRSelect(t *testing.T) {
arr := []int{5, 2, 6, 8, 3, 1}
i0 := RSelect(arr, 6, 0)
i1 := RSelect(arr, 6, 1)
i2 := RSelect(arr, 6, 2)
i3 := RSelect(arr, 6, 3)
i4 := RSelect(arr, 6, 4)
i5 := RSelect(arr, 6, 5)
if i0 != 1 ||
i1 != 2 ||
i2 != 3 ||
i3 != 5 ||
i4 != 6 ||
i5 != 8 {
fmt.Println("Error: ", i0, i1, i2, i3, i4, i5)
t.Error()
}
}