Skip to content

Linear Search

Linear search or sequential search is a method for finding a value whithin a list. This algorithm sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.

This algorithm has the time complexity of O(n) since in worst case scenario it'll check each element once.

public static Integer linearSearch(ArrayList<Integer> array, int target) {
    for (int i = 0; i < array.size(); i++) {
        if (array.get(i) == target) {
            return i;
        }
    }
    return null;
}