Today I learned about Linear Search and Binary Search.
First, letβs talk about Linear Search.
It is an algorithm used to search for an element in an array.
If we go deeper, Linear Search checks elements one by one from start to end until:
The element is found, or
The list ends.
Letβs take a real-life example:
If I am searching for the name "Lakhan" and it is present at index 4, then the 0th, 1st, 2nd, and 3rd iterations will fail. At the 4th iteration, it will find the element, return it, and stop the loop.
Thatβs how Linear Search works.
Now, letβs talk about Binary Search.
On the internet, it says Binary Search is the fastest and most optimized way to search for an element. Basically, it is an optimized way to search an element in a sorted array.
It does not work on an unsorted array.
If we go deeper, Binary Search divides the array into halves each time. It checks the middle element and continues the search only in the relevant half, while ignoring the other half.
This process continues until the element is found or the search space becomes empty.
Top comments (0)