findMin
Table of Contents

Definition

In order to find the location of the smallest value of an unsorted array, you need to use the findMin method, which loops through the array to find the minimum.

Algorithm

1. Assume the zeroth value of the array is the minimum.
2. Proceed through the array, checking if any value is less than the already assumed.
3. If a value less than the assumed is found, assume that location to be the new minimum.
4. Repeat steps 2 & 3 until you reach the end of the array.

Code

public static int findMin(int[] x, int start){
    int min = start;
    for(int i = start + 1; i < x.length; i++){
        if (x[i] < x[min]){
            min = i;
        }
    }
    return min;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License