Linear Search Algorithm

The Linear Search Algorithm, also known as the Sequential Search Algorithm, is a fundamental searching technique used for finding a specific element within a list or an array of elements. The algorithm works by iterating through each element in the list or array, comparing it with the target value. If the element being examined matches the target value, the algorithm stops and returns the index of the matching element. If the algorithm reaches the end of the list or array without finding the target element, it returns an indication that the value was not found, such as -1 or null. The primary advantage of the Linear Search Algorithm is its simplicity, as it requires no prior knowledge of the data structure or any specific ordering of the elements within the list or array. This makes it particularly useful for small datasets, unsorted lists, or when searching for multiple occurrences of a target value. However, the algorithm's performance can be quite slow for large datasets, as it has a worst-case time complexity of O(n), where n is the number of elements in the list or array. In such cases, more efficient search algorithms like Binary Search or Hash-based searching techniques can be employed for better performance.
package Searches;

import java.util.Random;
import java.util.stream.Stream;

/**
 * Linear search is the easiest search algorithm
 * It works with sorted and unsorted arrays (an binary search works only with sorted array)
 * This algorithm just compares all elements of an array to find a value
 * <p>
 * Worst-case performance	O(n)
 * Best-case performance	O(1)
 * Average performance	O(n)
 * Worst-case space complexity
 *
 * @author Varun Upadhyay (https://github.com/varunu28)
 * @author Podshivalov Nikita (https://github.com/nikitap492)
 * @see BinarySearch
 * @see SearchAlgorithm
 */

public class LinearSearch implements SearchAlgorithm {

    /**
     * Generic Linear search method
     *
     * @param array List to be searched
     * @param value Key being searched for
     * @return Location of the key
     */
    @Override
    public <T extends Comparable<T>> int find(T[] array, T value) {
        for (int i = 0; i < array.length; i++) {
            if (array[i].compareTo(value) == 0) {
                return i;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        //just generate data
        Random r = new Random();
        int size = 200;
        int maxElement = 100;
        Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[]::new);


        //the element that should be found
        Integer shouldBeFound = integers[r.nextInt(size - 1)];

        LinearSearch search = new LinearSearch();
        int atIndex = search.find(integers, shouldBeFound);

        System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
                , shouldBeFound, integers[atIndex], atIndex, size));
    }

}

LANGUAGE:

DARK MODE: