mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-07-01 13:06:29 +00:00
Update 4Galloping.md
This commit is contained in:
parent
7dbc681720
commit
127de198a4
@ -3,21 +3,21 @@
|
|||||||
Standard merging procedure for merging two sorted arrays array_1: [10] and array_2: [1,2,3,4,6,9,14] goes as below:
|
Standard merging procedure for merging two sorted arrays array_1: [10] and array_2: [1,2,3,4,6,9,14] goes as below:
|
||||||
|
|
||||||
CREATE GIF
|
CREATE GIF
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
As you can see we are consistently taking elements from array_2 until we reach $14$. But can we do better? Yes, use galloping.
|
As you can see we are consistently taking elements from array_2 until we reach $14$. But can we do better? Yes, use galloping.
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ Note that we can do galloping(exponential search) from any side of the array, ei
|
|||||||
|
|
||||||
The starting position for the search is called a "$hint$". Sometimes it is better to search from the left and sometimes from the right. For the given array below,
|
The starting position for the search is called a "$hint$". Sometimes it is better to search from the left and sometimes from the right. For the given array below,
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
If we want to find position for $3$, then starting from index 0($hint = 0$) is efficient, but if we are looking for position of $13$ then starting from index $len-1$($hint = len-1$) is more efficient. **It is particularly efficient for big arrays.**
|
If we want to find position for $3$, then starting from index 0($hint = 0$) is efficient, but if we are looking for position of $13$ then starting from index $len-1$($hint = len-1$) is more efficient. **It is particularly efficient for big arrays.**
|
||||||
|
|
||||||
@ -63,13 +63,13 @@ We have two types of galloping function `gallopRight` and `gallopLeft`, the main
|
|||||||
|
|
||||||
For example, for the array given below,
|
For example, for the array given below,
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
If you find a position of 13 by using galloping, then `gallopLeft` will return 6, but `gallopRight` will return 9.
|
If you find a position of 13 by using galloping, then `gallopLeft` will return 6, but `gallopRight` will return 9.
|
||||||
|
|
||||||
In simple mode, it is trivial to maintain stability, but to maintain stability while merging in galloping mode, we sometimes use `gallopRight` and sometimes `gallopLeft`. Just to get the basic idea, see the below example, it will be more clear when we will the merge procedure.
|
In simple mode, it is trivial to maintain stability, but to maintain stability while merging in galloping mode, we sometimes use `gallopRight` and sometimes `gallopLeft`. Just to get the basic idea, see the below example, it will be more clear when we will the merge procedure.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Now, if we are finding a position of run2[0] in run1, then we will use `gallopRight`, but if we are finding a position for run1[3] in run2, then we will use `gallopLeft`.
|
Now, if we are finding a position of run2[0] in run1, then we will use `gallopRight`, but if we are finding a position for run1[3] in run2, then we will use `gallopLeft`.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user