使用快速排序分区的中值中值规则出现越界错误
我正在使用 中值中位数 算法来选择第 k 个元素href="https://rads.stackoverflow.com/amzn/click/com/0763782505" rel="nofollow noreferrer">算法基础,我在用java实现它时遇到了麻烦。我收到数组越界错误,想知道是否有人可以帮助我正确实现该算法。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test2.selection2(test2.java:23)
at test2.select4(test2.java:16)
at test2.partition2(test2.java:55)
at test2.selection2(test2.java:27)
at test2.select4(test2.java:16)
at test2.partition2(test2.java:55)
at test2.selection2(test2.java:27)
at test2.select4(test2.java:16)
at test2.main(test2.java:11)
这些是变量的值:
N size = 10
low = 0
high = 10
k = 3
arraysize = 10
r = 2
i = 1,2,3
first = 0,5,10
last = 4,9,11
lower = 7, 33
upper = 10, 44
-pivotitem
T size = 2
low = 0
high = 2
k = 1
arraysize = 10
r = 0
high==low [0]
list is empty
由于我的数组从大小 10 开始,因此 r 将为 2。当从ivotitem 再次调用partition2 时,r 将为 0,从而生成大小为 0 的数组 T。然后 low 和 high 将等于 0 ,什么也不返回,这就是我收到错误的地方。我不知道为什么会发生这种情况,因为我的代码与书中的算法类似。
I am following a selection of kth element using median of median algorithm from Foundations of Algorithms and I am having trouble implementing it in java. I am getting an array out of bounds error and was wondering if someone can help me implement this algorithm correctly.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test2.selection2(test2.java:23)
at test2.select4(test2.java:16)
at test2.partition2(test2.java:55)
at test2.selection2(test2.java:27)
at test2.select4(test2.java:16)
at test2.partition2(test2.java:55)
at test2.selection2(test2.java:27)
at test2.select4(test2.java:16)
at test2.main(test2.java:11)
These are the values of the variables:
N size = 10
low = 0
high = 10
k = 3
arraysize = 10
r = 2
i = 1,2,3
first = 0,5,10
last = 4,9,11
lower = 7, 33
upper = 10, 44
-pivotitem
T size = 2
low = 0
high = 2
k = 1
arraysize = 10
r = 0
high==low [0]
list is empty
Since my array starts at size 10, r will be 2. When partition2 is called again from pivotitem, r will be 0, resulting in a array T of size 0. Then low and high will equal 0, returning nothing, which is where I am getting my error. I dont know why this is happening since my code is similar to the algorithm in the book.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的交换方法看起来好像它以索引作为参数,但您向它提供了值
这不是错误的根源,并且在更改调用后错误仍然存在,但也许您多次出错。顺便说一句:代码去哪里了?
Your swap method looks as if it takes indexes as argument, but you feed it with values
That's not the root of your error, and the error persists after changing the invokations, but maybe you got it multiple times wrong. BTW: Where did the code go?
原来是C-Code?使用 & 调用partition2
这意味着,一个引用,即改变的结果被调用者看到。
您似乎使用静态“thepivotpoint”来解决这个问题,但是您不能将其用作参数;它只会隐藏静态成员。
它还没有完成。
The original is C-Code? partition2 is called with &
which means, a reference, that is, the changed result is seen by the caller.
You seemed to adress that with a static 'thepivotpoint', but then you can't use it as parameter; it will just hide the static member.
It is still not complete.
我还有一些其他改进,没有解决方案,但也许是进一步发展的更好基础:
(int[], int, ...)
if ... return else ...
中删除了 else。show
的方法,该方法会触发计数器,以停止无限循环。删除了对我没有帮助的评论。也许您可以添加更好的评论。
I have some other improvements, without solution, but maybe a better basis to go further:
(int[], int, ...)
if ... return else ...
.show
which triggers a counter, to stop in an endless loop.removed comments which didn't helped me. Maybe you can add better comments.