Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的排序算法问题;得到错误的结果_Java_Arrays_Algorithm_Sorting - Fatal编程技术网

Java中的排序算法问题;得到错误的结果

Java中的排序算法问题;得到错误的结果,java,arrays,algorithm,sorting,Java,Arrays,Algorithm,Sorting,在类Main程序中创建一个名为indexOfSmallestFrom的类方法。它的工作原理与上一节中的方法类似,但只考虑来自某个索引的表值。除了表之外,它还接收此起始索引作为参数 在本例中,第一个方法调用从索引0开始搜索最小数字的索引。从索引0开始,最小的数字为-1,其索引为0。第二个方法调用从索引1开始搜索最小值的索引。在这种情况下,最小的数字是6,其索引是1。第三个调用从索引2开始搜索最小值的索引。那么最小的数字是8,它的索引是3 应为最小数组打印索引。 根据int数组,我的输出应该是: 1

在类Main程序中创建一个名为indexOfSmallestFrom的类方法。它的工作原理与上一节中的方法类似,但只考虑来自某个索引的表值。除了表之外,它还接收此起始索引作为参数

在本例中,第一个方法调用从索引0开始搜索最小数字的索引。从索引0开始,最小的数字为-1,其索引为0。第二个方法调用从索引1开始搜索最小值的索引。在这种情况下,最小的数字是6,其索引是1。第三个调用从索引2开始搜索最小值的索引。那么最小的数字是8,它的索引是3

应为最小数组打印索引。 根据int数组,我的输出应该是:

1
1
4
但我得到:

1
1
3
不明白为什么

public class MainProgram {

    public static void main(String[] args) {

        int[] array = {3, 1, 5, 99, 3, 12};
        
        System.out.println(MainProgram.indexOfSmallestFrom(array, 0));
        System.out.println(MainProgram.indexOfSmallestFrom(array, 1));
        System.out.println(MainProgram.indexOfSmallestFrom(array, 2));
    }
    
    public static int indexOfSmallestFrom(int[] table, int startIndex){
        int count = startIndex;
        int indexOf = 0;
        int smallest = table[startIndex];
        for(int i = startIndex; i < table.length; i++){
            if(smallest > table[i]){
               smallest = table[i];
               count++;
            }
              indexOf = count; 
        }
            return indexOf;
    }   
 
}

当您发现一个元素小于初始初始化为startIndex的indexOf处的值时,只需更新indexOf

在遍历所有值之后。。。返回最后更新的indexOf

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {

        int[] array = {3, 1, 5, 99, 3, 12};

        System.out.println(indexOfSmallestFrom(array, 0));
        System.out.println(indexOfSmallestFrom(array, 1));
        System.out.println(indexOfSmallestFrom(array, 2));
    }

    public static int indexOfSmallestFrom(int[] table, int startIndex){
        int indexOf = startIndex;
        int smallest = table[startIndex];
        for(int i = startIndex; i < table.length; i++){
            if(smallest > table[i]){
               smallest = table[i];
               indexOf = i;
            }
        }
            return indexOf;
    }   
}

当您发现一个元素小于初始初始化为startIndex的indexOf处的值时,只需更新indexOf

在遍历所有值之后。。。返回最后更新的indexOf

// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {

        int[] array = {3, 1, 5, 99, 3, 12};

        System.out.println(indexOfSmallestFrom(array, 0));
        System.out.println(indexOfSmallestFrom(array, 1));
        System.out.println(indexOfSmallestFrom(array, 2));
    }

    public static int indexOfSmallestFrom(int[] table, int startIndex){
        int indexOf = startIndex;
        int smallest = table[startIndex];
        for(int i = startIndex; i < table.length; i++){
            if(smallest > table[i]){
               smallest = table[i];
               indexOf = i;
            }
        }
            return indexOf;
    }   
}

设置indexOf不应该只在遇到新的最小数时发生吗?您可能需要检查indexOf=count的逻辑位置;或者只要在找到新的最小项时保存变量I,那么你就不必使用count了。你的逻辑错误是,你现在实际返回startindex+在子数组中找到新最小值的次数-1,因为你赋值的变量是minister。设置indexOf不应该只在遇到新的最小值时发生吗?您可能需要检查indexOf=count的逻辑位置;或者,只要在找到新的最小项时保存变量I,就不必使用count。逻辑错误是,您现在实际返回startindex+在子数组中找到新最小项的次数-1,因为您分配的变量最小。非常感谢。这就是我过度思考的结果。@FrancisAdewale你能给我你的第一票吗?@YashShah哈哈,我想他错误地投了你一票xD@saurabhgupta要否决投票,一个人必须至少有125个代表点。@YashShah是的,我的坏消息非常感谢。这就是我过度思考的结果。@FrancisAdewale你能给我你的第一票吗?@YashShah哈哈,我想他错误地投了你一票xD@saurabhgupta要否决投票,一个人必须至少有125个代表点。@YashShah是的,我的错