开发者

What is Lazy Binary Search?

开发者 https://www.devze.com 2023-03-04 19:57 出处:网络
I don\'t know whether the term \"Lazy\" Binary Search is valid, but I was going through some old materials and I just wanted to know if anyone can explain the algorithm of a Lazy Binary Search and com

I don't know whether the term "Lazy" Binary Search is valid, but I was going through some old materials and I just wanted to know if anyone can explain the algorithm of a Lazy Binary Search and compare it to a non-lazy Binary Search.

Let's say, we have this array of numbers:

2, 11, 13, 21, 44, 50, 69, 88

How to look for the number 1开发者_运维问答1 using a Lazy Binary Search?


Justin was wrong. The common binarySearch algorithm first checks whether the target is equal to current middle entry before proceeding to the left or right halves if required. Lazy binarySearch algorithm postpones the equality check until the very end.

algorithm lazyBinarySearch(array, n, target)
left<-0
right<-n-1
while (left<right) do
    mid<-(left+right)/2
    if(target>array[mid])then
        left<-mid+1
    else
        right<-mid
    endif
endwhile

if(target==array[left])then
    display "found at position", left
else
    display "not found"
endif

In your case, in an array,

2 11 13 21 44 50 69 88

and you want to search for 11

First we do a trace of common binary search,

index 0     1  2  3   4  5  6  7
      2     11 13 21  44 50 69 88
      left        mid          right

First while loop:

left <= right, we enter the first while loop. We calculated the mid index by (0+7)/2=3.5=3 by integer division, mid = 3. straight away we check if target 11 is equal to the mid index entry, 11 != 21, then we decide whether to go left or right, we finds out 11 < 21, should go left. left index remains unchanged, right index becomes mid index -1, right = 3 - 1 = 2. Done this step.

Second while loop:

left <= right, 0 <= 2, we enter the seond while loop. Mid index is recalcuated: (0+2)/2=1, mid = 1. At once we do the equality check, target 11 is the same as the index 1 entry, 11 == 11. We found this entry, leaving behind all the left right mid indexes (don't care) and breaks out the while loop, return index 1.

Now we trace this search by lazy binazySearch algorithm, initial array with left/right indexes set up the same as previous.

First while loop:

left < right, we enter the first while loop. Mid index is calculated as the same above = 3. Instead of doing an equality check in common binarySearch we do a comparison with the mid index entry this time. And the comparison only checks if our target 11 is greater than the mid index entry, leaving whether they equal or not to the very end outside the while loop. So we find out 11 < 21, right index is reset to the mid index, right = 3. Done this step.

Second while loop:

left < right, 0 < 3, we enter the second while loop. mid index is recalculated as mid = (0+3)/2 = 1 by integer division. Again we do a comparison with mid index entry 11, we realise it's not greater than mid index entry. We fall into the else part of the while loop and reset the right index to be mid index, right = 1. Done this step.

Third while loop:

left < right, 0 < 1, this time we have to re-enter the while loop again since it still satisfies the while condition. Mid index becomes (0+1)/2=0, mid = 0. After comparing target 11 with mid index entry 2 we found out it's greater than it (11 > 2), left index is reset to mid + 1, left = 0 + 1 = 1. Done this step.

With left index = 1 and right index = 1, left = right, while loop condition is no longer satisfied, so there's no need to re-enter. We fall into the if part down below. Target 11 equals left index entry 11, we found the target and returns left index 1.

As you can see, lazy binarySearch does one more while loop step to finally realise the index is actually 1. And this is how the word "postpones the equality check" means in the definition I mentioned in the very beginning. Clearly the lazy binarySearch algorithm does more things than common binarySearch before reaching the program termination. And the term "lazy" is reflected in the time of when to check the target equals the mid index entry.

However lazy binarySearch is more preferable to use under some other circumstances, but it's not in the context of this case.

(The reasoning part of the algorithm is done by me, anyone wishes to copy please do credit)

source: "Data Structures Outside In With Java" by Sesh Venugopal, Prentice Hall


As far as I am aware "Lazy binary search" is just another name for "Binary search".

0

精彩评论

暂无评论...
验证码 换一张
取 消