Skip to content

Latest commit

 

History

History
16 lines (8 loc) · 609 Bytes

File metadata and controls

16 lines (8 loc) · 609 Bytes

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.