mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 13:06:29 +00:00
Update SearchingAlgorithms.md
This commit is contained in:
parent
8be7d54b59
commit
f2cbc6bee6
@ -1,16 +1,16 @@
|
||||
## Exponential Search
|
||||
|
||||
Exponential search is an algorithm for searching in a sorted list. As we know for a sorted list of size $n$, binary search works in $\log{(n)}$, but can we do better that this?
|
||||
Exponential search is an algorithm for searching in a sorted list. For a sorted list of size $n$, binary search works in $\log{(n)}$, but can we do better that this?
|
||||
|
||||
Apparantely, no. But what if we use some mechanism to determine a smaller index-range in which the element we are searching(say $X$) belongs?
|
||||
Apparantely, no. But what if we use some mechanism to determine a smaller index-range, in which the element we are searching(say $X$) belongs?
|
||||
|
||||
For example, we are searching for $8$ in the list ${2,4,5,8,9,10}$. Now, if we use binary search then the searching index-range will be $1$ to $6$. But we can see that if we binary search in the index-range $3$ to $6$ then also we will be able to find $8$.
|
||||
|
||||
Note that the number of comparisons for index-range $3$ to $6$ is lesser than the ordinary binary search, this is the main idea behind the **Exponential search**.
|
||||
Note that the number of comparisons for smaller index-range is lesser than the ordinary binary search, this is the main idea behind **Exponential search**.
|
||||
|
||||
But how do we determine the index-range for a given element?
|
||||
But how do we determine such index-range for a given element?
|
||||
|
||||
As the name of the algorithm suggests, we are going to use exponentiation. We find an index of form $2^k$(starting from $k$=$0$), such that the element at that index is greater than $X$.
|
||||
As the name of the algorithm suggests, we are going to use exponentiation. We find an index of form $2^k$(starting from $k$=$0$), such that the element at that index is greater than $X$.
|
||||
|
||||
After that we do binary search over the index-range $2^{k-1}$ to $2^k$. It is easy to see that $X$ is in the index-range $2^{k-1}$ to $2^k$, because element at index $2^{k-1}$ is lesser then $X$ and element at index $2^k$ is greater than $X$.
|
||||
|
||||
@ -78,21 +78,26 @@ Can you figure out the best time complexity?
|
||||
### When to use exponential search?
|
||||
Worst case time-complexity of exponential search is $\log(n)$.
|
||||
|
||||
We can see that, when the element is near to the front of the list, exponential search performs better than binary search.
|
||||
We can see that, when the element is near to the front of the list, exponential search performs better than binary search for the case of very large list.
|
||||
|
||||
**Therefore, when the list size is very big and it looks like the element is too far from the end of the list, use exponential search.**
|
||||
|
||||
Therefore, when the list size is very big and it looks like the element is too far from the end of the list, use exponential search.
|
||||
|
||||
## Interpolation Search
|
||||
|
||||
Suppose, we have a very long sorted list and we are searching $X$. Let say, we have a mechanism that takes us directly to an index near to $X$, rather than to the middle element of the range, in case of binary search.
|
||||
Interpolation is a well known concept in mathematics, which is used in many fields of engineering and science for estimations. Here also we are going to use it to determine an approximate index for a key.
|
||||
|
||||
**For example**, we are looking for a key which is near to the end of a long list. So, by this mechanism it will indicate that we should start searching close to the end of the list.
|
||||
Suppose, we have a very long sorted list and we are searching for a key-$X$. Let say, we have a mechanism that takes us directly to an index near to $X$, rather than to the middle index of the range(in case of binary search), to determine which side to go next for searching.
|
||||
|
||||
**For example**, we are looking for a key which is near to the end of a long list. So, this mechanism will indicate that we should start searching close to the end of the list. This may lead to less amount of comparisons than ordinary binary search.
|
||||
|
||||
In binary search, we use simple formula $(l+r)/2$ to find the mid index. But now, we are going to use different formula. So the question is, how to determine such a formula which finds an index near to the key($X$)?
|
||||
|
||||
In order to find such a formula, we need to have some characteristics of data, other than it is sorted. Like, the given data follows linear distribution or exponential distribution or normal distribution or some other distributions.
|
||||
In order to find such a formula, we need to have some characteristics of data, other than it is sorted. Like, the given data follows linear distribution or exponential distribution or normal distribution or some other distributions.
|
||||
|
||||
For linear distribution case, the formula is as below:
|
||||
**Interpolation search works very well when the data follows some kind of distribution.**
|
||||
|
||||
For linear distribution case, the formula is as below, which is called **interpolation formula**.
|
||||
|
||||
$mid = low + ((key - arr[low]) * (high - low) / (arr[high] - arr[low]))$
|
||||
|
||||
@ -111,6 +116,7 @@ else
|
||||
return mid;
|
||||
|
||||
```
|
||||
For linear distribution case, the interpolation search for an array $[2, 3, 5, 8, 12, 14]$ and $X=5$ goes as follows:
|
||||
|
||||

|
||||
|
||||
@ -145,8 +151,8 @@ int interpolation_search(vector<int>& list, int key)
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> list{3,5,6,7,8,10,17,20,24,27};
|
||||
int key = 10;
|
||||
vector<int> list{2,3,5,8,12,14};
|
||||
int key = 5;
|
||||
|
||||
cout << interpolation_search(list,key) << endl;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user