find smallest number's index [closed]

风流意气都作罢 提交于 2019-12-08 12:15:25

问题


It's returning the second element in the array instead of the smallest number's index
I already took the size and all that stuff, this is just the method

public static int FindSmallest (int [] arr1){//start method

    int index = arr1[0];

    for (int i=1; i<arr1.length; i++){

        if (arr1[i] > index ){
            index = arr1[i];
        }

        return index ;
    }

    return 0;

}//end method

回答1:


How would you do on paper ?

  1. Initialize the minimum value with the first element of your array
  2. Initialize the corresponding index to 0 (arrays are 0 base indexed)
  3. Loop in your array
  4. If you find a number smaller than the minimum value, update the minimum value with the value found
  5. If 4 is satisfied, update the corresponding index with the current index
  6. Return the index
  7. You've done it.



回答2:


public static int FindSmallest (int [] arr1) {
       int index = 0;
       int min = arr1[index];

       for (int i=1; i<arr1.length; i++) {
           ...

           if (arr1[i] < min) {
               min = arr1[i];
               index = i;
           }
           ...
       }
       return index;
}



回答3:


Algorithm FindSmallest (arr1[])
// This Algorithm returns the index of smallest element in the array arr1
// Assumption : index of arr1 starts from 0
//              arr1.length returns the length of arr1
begin
   set index := 0;
   set len   := arr1.length;
   set min   := arr1[index];
   For i:=1 to len,do
   begin
       if arr1[i] < min ,then
       begin
           min   := arr1[i];
           index := i;
       end
   end
   return index;
end



回答4:


Your problem is what you return.

A couple of things:

The array shouldn't be static, you should pass it as a parameter to the arrayMin method; min should be a local arrayMin variable, not static; min should be initialized to Integer.MAX_VALUE. If you initialize it with 1, and 2 happens to be the min value of the array, you'll never return it; You can't return multiple times from a method. As soon as you do return min, the method ends. There's probably some confusion over the the variable min will return the smallest number from the first i elements phrase. It probably means that in each iteration, the variable min will have (not return) the smallest number from the first i elements.

Here's a refactor:

public static int arrayMin(int[] arr1) {
        int i = 0;
        int min = Integer.MAX_VALUE;
        if (arr1 == null) {
            return 0; // What if 0 is the minimum value? What do you want to do in this case?
        } else {
            while (i < arr1.length) {
                if (arr1[i] < min) {
                  min = arr1[i];
                }
                i++;
            }
        }
        return min;
    }


来源:https://stackoverflow.com/questions/20332199/find-smallest-numbers-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!