英文:
Java: Increment operation not working inside if block
问题
在这一行中 if ( intArray[index] > intArray[index+1]) {,我将 index+1 作为参数传递才能使我的代码正常工作,如果我传递 i++ 或 ++i,它就不起作用。
这种行为背后的原因是,index+1 表示的是数组中的下一个元素,而 i++ 或 ++i 表示的是对索引 i 的自增操作。在冒泡排序算法中,你需要比较相邻的元素,因此使用 index+1 是正确的,因为它总是指向当前元素的下一个元素,而不会改变当前元素的索引。如果你使用 i++ 或 ++i,它会导致在比较时跳过元素,因为它会修改 i 的值,使其指向下一个元素,从而破坏了冒泡排序的逻辑。
所以,使用 index+1 是正确的选择,因为它保持了正确的元素比较顺序。
英文:
I am writing a small code for bubble sort using Java as below
package raja.programming.bubblesort;
public class Main {
public static void main(String[] args) {
// write your code here
//int[] intArray = {5, -10, 22, 43, 1, 17};
int[] intArray = { 20, 35, -15, 7, 55, 1, -22 };
for ( int unsortedArrayIndex = intArray.length -1 ; unsortedArrayIndex > 0 ; unsortedArrayIndex--){
for ( int index = 0 ; index < unsortedArrayIndex ; index++){
System.out.println("index " + intArray[index] + " index+1 : " + intArray[index+1] );
if ( intArray[index] > intArray[index+1]) {
swap(intArray, index, index+1);
}
}
}
for (int num: intArray) {
System.out.println(num);
}
}
public static void swap( int[] array, int i, int j) {
if ( i == j) {
return ;
}
System.out.println("swap index " + array[i] + " index+1 : " + array[j] );
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
In the line if ( intArray[index] > intArray[index+1]) { I am passing as index+1 then only my code working, if I pass i++ or ++i its not working.
What is the reason behind this behaviour, please help me understand?
Thank you.
答案1
得分: 1
当你使用 index + 1 时,你的代码会比较位于 index 位置和位于 index + 1 位置的元素,并且循环在循环内部指令的末尾添加1。
但是,如果你使用 ++i 或 i++,你的代码会执行相同的操作,但你使用的索引会增加2,增加了for循环和你自己的增加。
如果你使用 i++,它不会起作用,因为这会增加 i 并返回增加之前的 i 值,所以就好像你在比较元素与自己。
但如果你使用 ++i,它会增加 i 的值并返回增加后的值。但无论哪种方式,在一个循环中,你都会增加 i 两次。如果你真的想使用其中之一,你需要移除for循环,使用一个while循环,并使用 ++i。
英文:
When you use index + 1 your code compares the element at the position index and the one at the position index +1 and the loop adds 1 at the end of the instuctions inside the loop
But if you use ++i or i++ your code will do the same thing but the index you used will jump by 2 the incrementation of the for loop and the incrementation you did.
it will not work if you do i++ beacuse this will increment i and return the previous value of i before incrementing so it's like you compare the element to itself
But if you use ++i it will increment the value of i and return the value incremented. But in both ways, in one loop, you increment i twice. If you really want to use either of those you need to remove the for loop and use a while loop and use ++i
答案2
得分: 0
我认为原因是,如果您在循环中使用索引i并在循环内递增它,您将跳过下一次迭代。i++等同于i = i+1,但i+1不等同于i=i+1。
英文:
i think the reason is. if u are using an index i for example and u increment it inside the loop, u will jump the next iteration. i++ is equals to i = i+1, but i+1 is not equals to i=i+1
答案3
得分: 0
你正在使用 index 进行迭代,当你在数组中引用位置时使用 index + 1 时,index 变量的值没有改变。
但是如果你使用 index++ 或 ++index,那么在该迭代中你会将 index 变量的值增加 1,这将导致跳过一个 index。因为同样的原因,所以在这种情况下,你得到的输出数组是无序的。
英文:
You are iterating using index, when you are referring the position in an array using index + 1then the value ofindex` variable is not changed.
But if you use index++ or ++index, then you are incrementing the value of the index variable by 1 in that iteration and you will be skipping one index. Because of the same reason, the output array you are getting is unsorted in this case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论