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

查找第K小元素java代码问题:以边界 0 和数组末尾作为循环查找参数,会导致重复查找 #493

Open
zhangwade opened this issue Nov 13, 2020 · 1 comment

Comments

@zhangwade
Copy link

if (partition + 1 < k) {
partition = partition(arr, partition + 1, arr.length - 1);
} else {
partition = partition(arr, 0, partition - 1);

@zhangwade zhangwade changed the title 查找第K小元素java代码问题:以边界 0 和数组末尾作为循环查找条件,会导致重复查找 查找第K小元素java代码问题:以边界 0 和数组末尾作为循环查找参数,会导致重复查找 Nov 13, 2020
@zhangwade
Copy link
Author

比如第 12 课快排的例子,[11, 8, 3, 9, 7, 1, 2, 5] 找第 3 小元素,以最后的元素作为 pivot,正常递归 2 次可以返回 3,但这里在 while 循环里跑了大于 2 次。因为 partition() 函数以 0 开头/arr.length - 1 结尾。

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