使用选择排序对 java 中的整数数组进行排序。找不到我的错误
Sorting an integer array in java, using selection sort. Cannot find my error
我正在尝试通过 'easy' 练习来学习代码。我正在尝试使用选择排序进行搜索算法。当我按照我脑海中的代码进行操作时,它非常有意义,但是当我 运行 它时,它不会订购任何东西。对于数组,我使用的是纯整数数组,它由随机数组成,长度随机。
int currentMin;
int currentMinIndex = 0;
int temp;
for(int i=0;i<array.length-1;i++){
currentMin = array[i];
for(int j=i+1;j<array.length-1;j++){
if(array[j]<currentMin){
currentMinIndex = j;
currentMin = array[j];
}
}
temp = array[currentMinIndex]; //I am aware this could be currentMin
array[currentMinIndex] = array[i];
array[i] = temp;
}
希望有人能指出我的错误并告诉我。
(如果你有我可以做的其他 'easy' 练习,请发挥创造力,但是这个 post 必须切题)
编辑:我刚刚注意到,当数组很长时,它以某种奇怪的方式排序,但排在最后一个。 (数组长度不同,因为它们是随机的)
你的两个循环都跳过了最后一个元素。考虑在循环条件中使用 <=
而不是 <
来考虑最后一个元素。
当你设置currentMin
时,你需要更新currentMinIndex
到i
,你的内循环应该是array.length
。
currentMin = array[i];
currentMinIndex = i;
for(int j=i+1;j<array.length;j++){
您可以通过将您的声明移动到循环中并删除 temp
(因为您有 currentMin
)来进一步简化它,例如
for (int i = 0; i < array.length - 1; i++) {
int currentMin = array[i];
int currentMinIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < currentMin) {
currentMinIndex = j;
currentMin = array[j];
}
}
array[currentMinIndex] = array[i];
array[i] = currentMin;
}
有两个问题
- 您忘记重置 currentMinIndex
- 内部for循环的边界条件不对
这是您的代码的修改版本:
public class SelectionSort {
public static void main(String[] args) {
int currentMin;
int currentMinIndex = 0;
int temp;
int[] array = {9, 1, 3, 2, 4, 7};
for(int i=0;i<array.length-1;i++){
currentMin = array[i];
currentMinIndex = i; // Problem #1
for(int j=i+1;j<=array.length-1;j++){ // Problem #2
if(array[j]<currentMin){
currentMinIndex = j;
currentMin = array[j];
}
}
temp = array[currentMinIndex]; //I am aware this could be currentMin
array[currentMinIndex] = array[i];
array[i] = temp;
}
StringBuilder sb = new StringBuilder();
sb.append("[");
String comma = ""; // first time special
for (int i=0; i<array.length; i++) {
sb.append(comma + array[i]);
comma = ", "; // second time and onwards
}
sb.append("]");
System.out.println(sb.toString());
}
}
这个程序的输出是
[1, 2, 3, 4, 7, 9]
我正在尝试通过 'easy' 练习来学习代码。我正在尝试使用选择排序进行搜索算法。当我按照我脑海中的代码进行操作时,它非常有意义,但是当我 运行 它时,它不会订购任何东西。对于数组,我使用的是纯整数数组,它由随机数组成,长度随机。
int currentMin;
int currentMinIndex = 0;
int temp;
for(int i=0;i<array.length-1;i++){
currentMin = array[i];
for(int j=i+1;j<array.length-1;j++){
if(array[j]<currentMin){
currentMinIndex = j;
currentMin = array[j];
}
}
temp = array[currentMinIndex]; //I am aware this could be currentMin
array[currentMinIndex] = array[i];
array[i] = temp;
}
希望有人能指出我的错误并告诉我。 (如果你有我可以做的其他 'easy' 练习,请发挥创造力,但是这个 post 必须切题)
编辑:我刚刚注意到,当数组很长时,它以某种奇怪的方式排序,但排在最后一个。 (数组长度不同,因为它们是随机的)
你的两个循环都跳过了最后一个元素。考虑在循环条件中使用 <=
而不是 <
来考虑最后一个元素。
当你设置currentMin
时,你需要更新currentMinIndex
到i
,你的内循环应该是array.length
。
currentMin = array[i];
currentMinIndex = i;
for(int j=i+1;j<array.length;j++){
您可以通过将您的声明移动到循环中并删除 temp
(因为您有 currentMin
)来进一步简化它,例如
for (int i = 0; i < array.length - 1; i++) {
int currentMin = array[i];
int currentMinIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < currentMin) {
currentMinIndex = j;
currentMin = array[j];
}
}
array[currentMinIndex] = array[i];
array[i] = currentMin;
}
有两个问题
- 您忘记重置 currentMinIndex
- 内部for循环的边界条件不对
这是您的代码的修改版本:
public class SelectionSort {
public static void main(String[] args) {
int currentMin;
int currentMinIndex = 0;
int temp;
int[] array = {9, 1, 3, 2, 4, 7};
for(int i=0;i<array.length-1;i++){
currentMin = array[i];
currentMinIndex = i; // Problem #1
for(int j=i+1;j<=array.length-1;j++){ // Problem #2
if(array[j]<currentMin){
currentMinIndex = j;
currentMin = array[j];
}
}
temp = array[currentMinIndex]; //I am aware this could be currentMin
array[currentMinIndex] = array[i];
array[i] = temp;
}
StringBuilder sb = new StringBuilder();
sb.append("[");
String comma = ""; // first time special
for (int i=0; i<array.length; i++) {
sb.append(comma + array[i]);
comma = ", "; // second time and onwards
}
sb.append("]");
System.out.println(sb.toString());
}
}
这个程序的输出是
[1, 2, 3, 4, 7, 9]