在java中使用选择排序对整数数组进行排序。找不到我的错误

在java中使用选择排序对整数数组进行排序。找不到我的错误,java,arrays,sorting,selection-sort,Java,Arrays,Sorting,Selection Sort,我试图通过做“简单”的练习来学习代码。我正在尝试使用选择排序进行搜索算法。当我在头脑中遵循代码时,它是非常有意义的,但当我运行它时,它不会命令任何东西。对于数组,我使用的是纯整数数组,它由随机数组成,长度是随机的 int currentMin; int currentMinIndex = 0; int temp; for(int i=0;i<array.length-1;i++){ currentMin = array[i]; fo

我试图通过做“简单”的练习来学习代码。我正在尝试使用选择排序进行搜索算法。当我在头脑中遵循代码时,它是非常有意义的,但当我运行它时,它不会命令任何东西。对于数组,我使用的是纯整数数组,它由随机数组成,长度是随机的

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;
    }
intcurrentmin;
int currentMinIndex=0;
内部温度;

对于(int i=0;i 两个循环都跳过最后一个元素。考虑使用<代码> < p>两个循环都跳过最后一个元素。考虑使用<代码> < p>,当您设置<代码> Currurmin MIN< /COD>时,需要更新<代码> CurrutMINCINDEX <代码> <代码> I/COD>,并且您的内循环应该是<代码>数组。长度

currentMin = array[i]; 
currentMinIndex = i;
for(int j=i+1;j<array.length;j++){

设置
currentMin
时,需要将
currentMinIndex
更新为
i
,并且内部循环应为
array.length

currentMin = array[i]; 
currentMinIndex = i;
for(int j=i+1;j<array.length;j++){

有两个问题

  • 您忘了重置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());
        }
    }
    

    有两个问题

  • 您忘了重置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
    )我已经写下来了,然后写下了每个变量。所有警告都打开了。我想我已经分解了代码,idk如何进一步。我已经完成了测试用例部分。我很抱歉这么傻。另外,你有两个变量currentMin和currentMinIndex,currentMinIndex就足够了。如果你将currentMin设置为array[i],然后您还必须将currentMinIndex设置为i。如果您使用调试器逐步检查代码,这一切都会非常明显。@4castle没问题,但我仍然得到未排序的数组。我刚刚将长度更改为5,以便更好地看到它。但有时它是排序的,有时则不是。我使用的是完全随机的nnumbers…我有消息告诉你了吗!内部循环需要转到
    数组。长度
    (删除
    -1
    )我已经写下来了,然后写下了每个变量。所有警告都打开了。我想我已经分解了代码,idk如何进一步。我已经完成了测试用例部分。我很抱歉这么傻。另外,你有两个变量currentMin和currentMinIndex,currentMinIndex就足够了。如果你将currentMin设置为array[i],然后您还必须将currentMinIndex设置为i。如果您使用调试器逐步检查代码,这一切都会非常明显。@4castle没问题,但我仍然得到未排序的数组。我刚刚将长度更改为5,以便更好地看到它。但有时它是排序的,有时则不是。我使用的是完全随机的n编号。。。