I need to get the index value of the minimum value in my arraylist in Java. MY arraylist holds several floats, and I\'m trying to think of a way I can get the index number o
You have to traverse the whole array and keep two auxiliary values:
Suppose your array is called myArray. At the end of this code minIndex has the index of the smallest value.
var min = Number.MAX_VALUE; //the largest number possible in JavaScript
var minIndex = -1;
for (int i=0; i
This is assuming the worst case scenario: a totally random array. It is an O(n) algorithm or order n algorithm, meaning that if you have n elements in your array, then you have to look at all of them before knowing your answer. O(n) algorithms are the worst ones because they take a lot of time to solve the problem.
If your array is sorted or has any other specific structure, then the algorithm can be optimized to be faster.
Having said that, though, unless you have a huge array of thousands of values then don't worry about optimization since the difference between an O(n) algorithm and a faster one would not be noticeable.