mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-09-13 13:52:12 +00:00
Update Exponential_Search.md
This commit is contained in:
@@ -1,18 +1,18 @@
|
|||||||
## Exponential Search
|
## Exponential Search
|
||||||
|
|
||||||
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?
|
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 than 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?
|
Apparently, 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$.
|
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 smaller index-range is lesser than the ordinary binary search, this is the main idea behind **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 such index-range for a given element?
|
But how do we determine such an 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 the 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$.
|
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$.
|
||||||
|
|
||||||
Let's have an example,
|
Let's have an example,
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ Can you figure out the best time complexity?
|
|||||||
### When to use exponential search?
|
### When to use exponential search?
|
||||||
Worst case time-complexity of exponential search is $\mathcal{O}(\log(n))$.
|
Worst case time-complexity of exponential search is $\mathcal{O}(\log(n))$.
|
||||||
|
|
||||||
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.
|
When the element is near to the front of the list, exponential search performs better than binary search for the case of a very large list.
|
||||||
|
|
||||||
For an example purpose, Let say for below array we are searching for 3,`[1,3,4,6,10,14,18,20,22,23,25,28,30,33,36,39,40,41]`. Then it is easy to see that binary search certainly takes more comparisons then exponential search.
|
For an example purpose, Let say for below array we are searching for 3,`[1,3,4,6,10,14,18,20,22,23,25,28,30,33,36,39,40,41]`. Then it is easy to see that binary search certainly takes more comparisons then exponential search.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user