问题描述
我在做选择排序的例子时有点卡住了,所以我使用下面的逻辑并比较左侧的每个元素,如果它更高则移到右侧。
我不确定我是否遗漏了这个逻辑。
如果有人在调试此类问题时可以指导任何方法,我将不胜感激?
注意:我目前正在使用 Eclipse
/** * */ 包 com.datastructureandalgo.all;
/**
* @author Umair
*
*/
public class selectionSort {
/**
* @param args
*/
public static void main(String[] args) {
int[] arrayInt = { 20,35,-15,7,55,1,-22 };
for (int lastUnsortedindex = arrayInt.length - 1; lastUnsortedindex > 0; lastUnsortedindex--) {
int tempHighIndex = 0;
for (int i = 0; i <= lastUnsortedindex; i++) { // line 26
if (arrayInt[tempHighIndex] > arrayInt[i + 1]) {
tempHighIndex = i;
} else {
tempHighIndex = i + 1;
}
replace(arrayInt,tempHighIndex,lastUnsortedindex);
}
for(int i=0;i<arrayInt.length;i++) {
System.out.println(arrayInt[i]);
}
// Todo Auto-generated method stub
}
/**
* @param arrayInt
* @param tempHighIndex
* @param lastUnsortedArray
*/
private static void replace(int[] arrayInt,int tempHighIndex,int lastUnsortedArray) {
// Todo Auto-generated method stub
arrayInt[lastUnsortedArray] = arrayInt[tempHighIndex];
}
}
-Error in Console
线程“main”中的异常java.lang.Arrayindexoutofboundsexception: 索引 7 超出长度 7 的边界 com.datastructureandalgo.all.selectionSort.main(selectionSort.java:26)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)