
public class LinearSearch
{
    public static void main(String[] args)
    {
        int[] myList = {1, 4, 4, 2, 5, -3, 6, 2};

        int i = linearSearch(myList,  4); // Returns  1
        int j = linearSearch(myList, -4); // Returns -1
        int k = linearSearch(myList, -3); // Returns 5
    }

    /* ----------------------------------------------------
       The linear search algorithm to find key
       in the array list
       ---------------------------------------------------- */
    public static int linearSearch(int[] list, int key)
    {
        for ( int i = 0; i < list.length; i++ )
            if ( list[i] == key )
               return i;

        // key was not found in list[]
        return -1;
    }
}
