java我对如何正确地编写while循环感到困惑。如何确保它遍历数组中的所有值
结果应该是数组按降序排序,但我的代码在一次迭代后停止,不能对整个数组进行完全排序,我不知道如何修复它。我是初学者,请用简单的术语解释逻辑
public static int[] insertionSort(int[] array){
for(int i1 = 1; i1 < array.length; i1++){
int indexCurrent = i1;
boolean done = false;
while(indexCurrent <= (array.length -1) && done == false){
if(array[indexCurrent] > array[indexCurrent-1]){
int temp = array[indexCurrent-1];
array[indexCurrent-1] = array[indexCurrent];
array[indexCurrent] = temp;
indexCurrent++;
}else{
done = true;
}
}
}
返回数组; }
# 1 楼答案
你应该减少
indexCurrent