<< Algorithms

Linear Search

Canvas settings

This algorithm searches a data structure from the first element and sequentially works its way through until the specified item is found. This algorithm can be used in sorted and unsorted data structures. It has a time complexity of O(n) meaning that as the size of the data structure increases, the time it takes for the worst case scenario increases linearly.
				function linearSearch(arr, x){
    for(let i = 0; i < arr.length; i++){
        if(arr[i] == x){
            return i;
        }
    }	 
    return -1;
}