The problem : Given an array arr[]
of n elements, write a function to search a given element x
in arr[]
.
( Can be used for unsorted arrays )
It sequentially checks each element of the array for the target value until a match is found or until all the elements have been searched.
Time complexity = O(n)
Steps :
- start from the index 0 of an array and one by one, compare
x
with each element of the array. - if
x
matches with the element, so return the index. - if doesn't matches and the array has reached the limit, then return -1.
( It is used for sorted arrays )
Binary search is a array search algorithm that follows the paradigm of division and conquest. It performs successive divisions of the search space by comparing the searched element x
with the element in the middle of the array.
Time complexity = O(Log n)
Steps :
- Compare
x
with the middle element. - If
x
matches with middle element, return the mid index. - Else If
x
is greater than the mid element, thenx
can only search in right half after the mid element. So, go to the right half. - Else (
x
is smaller), go to the left half.
(image example will be added later)
( It is used for sorted arrays )
The basic idea is to check fewer elements of an array arr[]
by jumping ahead by fixed steps, a value j
, in place of searching all elements, and when arr[j]
is greater then x
, jump back once and make a linear, or whatever search you want, to find the position of x
.
The best fixed value is caulculated by the square root of the lenght of array that will be used.
Time complexity = O(√n)
The time complexity of the auxiliar search changes accordingly with the search used.
Steps : (the value of j
is updated in every iterarion by j+= sqrt(arr.lenght)
)
- Jump from index 0 to index
j
. - If
arr[j]
is equals tox
, then returnj
. - Else If
arr[j]
is greater thanx
, so jump back in array with the previous value ofj
and do a linear search, or a binary search, to find the position ofx
and then return it. - Else, so update the value of
j
and go to step 2. - If the value of
j
becomes greater thanarr.lenght
so return -1.
(image example will be added later)
( It is used for sorted arrays )
Interpolation search can go to different locations according the value of key being searched. For example if the value of key is closer to the last element, interpolation search is likely to start search toward the end side. It works like a search in a phone book, when you look at the person number in the phone book, starting by the letters closer to the name of the person searched.
Time complexity = O (log log n)) if elements are uniformly distributed
In worst case it can take upto O(n)
To find the position to be searched, it uses following formula:
pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
arr[] ==> Array where elements need to be searched
x ==> Element to be searched
lo ==> Starting index in arr[]
hi ==> Ending index in arr[]
Steps :
- Calculate the value of
pos
using the formula. - If it is a match, return the index of
x
. - If
x
is less thanarr[pos]
, calculate the probe position of the left sub-array, takinghi = pos - 1
. Otherwise calculate the same in the right sub-array, takinglo = pos + 1
. - Repeat until a match is found or the sub-array reduces to zero, and then return -1.
(image example will be added later)
( It is used for sorted arrays )
Exponential search allows for searching through a sorted, unbounded array for a specified value x
. The algorithm consists of two stages. The first stage determines a range in which x
would reside if it were in the array. In the second stage, a binary search is performed on this range.
Time complexity = O(√n)
The time complexity of the auxiliar binary search is O(Log n).
Steps : (Given an index j
that grows up with the formula j*=2
)
- If
arr[j] == x
then returnj
. - Else if
arr[j] > x
, do a binary search in the following rangej/2, j
. - Else, then update
j
and go to step 1. - If
j>array.lenght
andx
isn't founded, then return -1;
(image example will be added later)