paint-brush
Linear Search Vs Binary Searchby@promilaghoshmonty
16,751 reads
16,751 reads

Linear Search Vs Binary Search

by Promila GhoshJune 13th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

All programmers are familiar with Linear search and Binary Search. Generally, we use them to search for any element and its location. Today’s discussion is about the comparison of these two searching algorithms.
featured image - Linear Search Vs Binary Search
Promila Ghosh HackerNoon profile picture

All programmers are familiar with Linear search and Binary Search. Generally, we use them to search for any element and its location. Today’s discussion is about the comparison of these two searching algorithms.

1. Sequential:

The linear search follows sequence and Binary search doesn’t follow. The linear search starts searching from the starting to ending point. Binary searching starts from the middle point.

2. Sorted:

For binary search, we need sorted elements. Linear search does not need sorted elements.

It searches all the element in all position until it gets the desired elements.

3. Comparison:

The number of comparison in Binary Search is less than Linear Search as Binary Search starts from the middle for that the total comparison is log_2_N.

4. Time Complexity:

From the following image, we can understand the time complexity of an Algorithm.

The time complexity of Linear search is:

a_._ Best case = O(1)

b_._ Average case = n(n+1)/2n = O(n)

c_._ Worst case = O(n)

The time complexity of Linear search is:

a_._ Best case = O(1)

b_._ Average case = logn(logn+1)/2logn = O(logn)

c_._ Worst case = O(logn)

So we can assume that the time complexity of Binary search is less than Linear search.

5. Space Complexity:

The space complexity of Linear Search is O(1) and Binary Search is O(1).

So we can assume that when we need better complexity then we should use the Binary Search algorithm. We can’t apply Binary Search in searching elements in an unsorted list. Happy Coding!