Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSelect method not working as expected #22

Open
JuliaAlves opened this issue Jun 15, 2022 · 0 comments
Open

RSelect method not working as expected #22

JuliaAlves opened this issue Jun 15, 2022 · 0 comments

Comments

@JuliaAlves
Copy link

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()
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant