//

Linear Search

public class LinearSearch {

    static int search(int arr[], int el) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == el) {
                return i + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(search(new int[]{1, 2, 3, 4, 5}, 2));
        System.out.println(search(new int[]{1, 2, 3, 4, 5}, 0));
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *