Search Algorithms
Tags: datastructures, algorithms, search, programming
Published: 2020-10-19 06:00:24

Today, let us touch base on some fundamental concepts like search algorithms.

>In simple terms, searching is a process of looking up a particular data record in the database or in the collection of items. A search typically answers as true or false whether the particular data in which we are referring is found or not and perform the next action accordingly.

Commonly used algorithms for search are:
* Linear search
* Binary search
* Interpolation search

Let us understand them in detail with some example

###Linear Search Algorithm
_Linear Search Algorithm_ is the simplest and easiest form of the search algorithm. In this algorithm, a sequential search is made over all the items one by one to search for the targeted item. Each item is checked in sequence until the match is found. 

>If the match is found, the searched item is returned otherwise the search continues till the end.

To make it easy to understand, let us see understand linear search using a flow diagram and example.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/287ztid8wl9m9m40uuyw.png)

####Points to note:
1. Does not need sorted array list
2. Performs equality comparisons
3. The time complexity is O(n)
4. Time taken to search elements keeps increasing as the number of elements is increased.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4e7npr60nz3qlir2ik2y.png)

-----

###Binary Search Algorithm
In _Binary search algorithm_, it begins with an interval covering the whole array and diving it in half. 

>If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

To make it easy to understand, let us see understand binary search using flow diagram and example as below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t1oyvs7ikdw3uh25fccz.png)

####Points to note:
1. The array needs to be sorted
2. Performs ordering comparisons
3. Time complexity to O(log n).
4. Search is done to either half of the given list, thus cut down your search to half time

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0l0o9f6hf4dlbjfah7t9.png)

--------
###Interpolation search Algorithm

_Interpolation search_ is an improved version of binary search. This search algorithm works on the probing position of the required value. Binary Search always goes to the middle element to check. On the other hand, interpolation search may go to different locations according to the value of the key being searched. For example, if the value of the key is closer to the last element, the interpolation search is likely to start search toward the end side. To find the position to be searched, it uses the following formula.

`position = lowVal + [ (s-array[lowVal])*(highVal-lowVal) / (array[highVal]-array[lowVal]) ]`

`array[]` => Array where elements need to be searched
`s` => Element to be searched
`lowVal` => Starting index in arr[]
`highVal` => Ending index in arr[]

>Algorithm is same as binary search except that here we determine the probe position based on the above formula

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q8t79q7y7y8vx0531vgi.png)

####Points to note:
1. The array needs to be sorted
2. Improved version of Binary search algorithm
3. Time complexity to log(log(n))

----

Hope this article helps refresh your basic concepts. I will come up with more articles to revise and touch base on legendary and foundational concepts.