Skip to content

Files

Latest commit

c4257b1 · Oct 25, 2018

History

History
This branch is up to date with matthewsamuel95/ACM-ICPC-Algorithms:master.

LinearSearch

Linear Search

Given an array arr[] of n elements, write a function to search a given element x in arr[].

A simple approach is to do linear search, i.e

1.Start from the leftmost element of arr[] and one by one compare x with each element of arr[]

2.If x matches with an element, return the index.

3.If x doesn’t match with any of elements, return -1.

The time complexity of above algorithm is O(n).

Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search.