You are on page 1of 3

Topic- Linear Searching

Subject Name/Code- Data Structure/CD 303


Department of CSE-DS

Linear Searching:

Linear search is the simplest searching algorithm that searches for an element in a list in
sequential order. We start at one end and check every element until the desired element
is not found.

The searching of an element in the given array may be carried out in the following two
ways:
• Linear Search
• Binary Search

Linear Searching Algorithm:

• Linear Search is the simplest searching algorithm.


• It traverses the array sequentially to locate the required element.
• It searches for an element by comparing it with each element of the array one by
one.
• it is also called as Sequential Search.
• Linear search is a very simple search algorithm.
• A Sequential search is made over all items one by one.
• Every item is checked and if a match is found then that particular item is
returned.
• The search continues till the end of the data collection.
• The time complexity of Linear search algorithm is O(n)

Linear Search Algorithm is applied when:


• No information is given about the array.
• The given array is unsorted or the elements are unordered.
• The list of data items is smaller.
Algorithm: LINEAR(DATA, N, ITEM, LOC)
Here DATA is a linear array with N elements, and ITEM is a given item of
information. This algorithm finds the location LOC of ITEM in DATA or set LOC : = 0
if the search is unsuccessful.

1. [Insert ITEM at the end of DATA.] Sel DATA[N + 1] : = ITEM.


2. [Initialize counter.] Set LOC : = 1.
3. [Search for ITEM.]
Repeat while DATA[LOC] ≠ ITEM:
Set LOC:= LOC + 1.
[End of loop.]
4. [Successful?] If LOC = N + 1, then: Set LOC: = 0
5. Exit.
Time Complexity of Linear Search

Best case:
• The element being searched may be found at the first position.
• In this case, the search terminates in success with just one comparison.
• Thus in best case, linear search algorithm takes Ω(1) operations.

Worst case:
• The element being searched may be present at the last position or not present in
the array at all.
• The search terminates in success with n comparisons.
• Thus in worst case, linear search algorithm takes O(n) operations.

Linear Search Efficiency

• Linear Search is less efficient when compared with other algorithms like Binary
Search & Hash tables.
• The other algorithms allow significantly faster searching.

You might also like