binary-search

How many comparisons will binary search make in the worst case using this algorithm?

隐身守侯 提交于 2019-11-30 06:29:55
Hi there below is the pseudo code for my binary search implementation: Input: (A[0...n-1], K) begin l ← 0; r ← n-1 while l ≤ r do m ← floor((l+r)/2) if K > A[m] then l ← m+1 else if K < A[m] then r ← m-1 else return m end if end while return -1 // key not found end I was just wondering how to calculate the number of comparisons this implementation would make in the worst case for a sorted array of size n? Would the number of comparisons = lg n + 1? or something different? The worst-case in this case is, if the element K is not present in A and smaller than all elements in A. Then we have two

First occurrence in a binary search

廉价感情. 提交于 2019-11-30 03:09:34
I'm tinkering with some code and I realized something I never knew. A normal binary search will return a random index in a data set for a key that occurs more than once. How can I modify this code below to return the first occurrence ? Is this something people do? //ripped from the JDK public static int binarySearchValue(InvertedContainer.InvertedIndex[] a, long key) { return bSearchVal(a, 0, a.length, key); } private static int bSearchVal(InvertedContainer.InvertedIndex[] a, int fromIndex, int toIndex, long key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid =

Difference between basic binary search for upper bound and lower bound?

一世执手 提交于 2019-11-29 22:38:14
In the article http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binarySearch , the author discusses binary search. He makes a distinction between finding the lowest value where something is true, and the highest value where something is false. The array being searched looks something like: false false false true true I am curious as to why these two cases are different. Why can't you just find the lowest value which is true, then subtract one to find the highest value which is false? Edit2: Ok, so I understand lower vs upper bound. Now, I am struggling to understand, when

Why in Java (high + low) / 2 is wrong but (high + low) >>> 1 is not?

陌路散爱 提交于 2019-11-29 22:15:28
I understand the >>> fixes the overflow: when adding two big positive longs you may endup with a negative number. Can someone explain how this bitwise shift magically fixes the overflow problem? And how it is different than >> ? My suspicious: I think it has to do with the fact that Java uses two-compliments so the overflow is the right number if we had the extra space but because we don't it becomes negative. So when you shift and paddle with zero it magically gets fixed due to the two-compliments. But I can be wrong and someone with a bitwise brain has to confirm. :) In short, (high + low) >

Binary search to find the rotation point in a rotated sorted list

不打扰是莪最后的温柔 提交于 2019-11-29 21:49:45
I have a sorted list which is rotated and would like to do a binary search on that list to find the minimum element. Lets suppose initial list is {1,2,3,4,5,6,7,8} rotated list can be like {5,6,7,8,1,2,3,4} Normal binary search doesn't work in this case. Any idea how to do this. -- Edit I have one another condition. What if the list is not sorted?? polygenelubricants A slight modification on the binary search algorithm is all you need; here's the solution in complete runnable Java (see Serg's answer for Delphi implementation, and tkr's answer for visual explanation of the algorithm). import

Debugging and Binary Search

时光毁灭记忆、已成空白 提交于 2019-11-29 08:37:55
"Programming Pearls" in the column 2 ("AHA! Algorithm") talks about how binary search aids in various processes like sorting, tree traversals. But it mentions that binary seach can be used in "program debugging". Can somebody please explain how this is done? Another possibility is that you have a bug, and you know it wasn't there in your February release, but it was in your April release (or rather, your April release candidate -- you would never actually ship a bug to your users, right?). You can do a manual binary search through your revision-control history to narrow down when the bug was

Extension of Binary search algo to find the first and last index of the key value to be searched in an array

亡梦爱人 提交于 2019-11-29 08:07:48
The problem is to extend the binary search algorithm to find all occurrences of a target value in a sorted array in the most efficient way. Concretely speaking, the input of the algorithm is (1) a sorted array of integers, where some numbers may appear more than once, and (2) a target integer to be searched. The output of the algorithm should be a pair of index values, indicating the first and last occurrence of the integer in the array, if it does occur. The source code could be in c#, c, c++. Also, what is the max and min number of comparisons that we might need to find the indexes? If you

How do you calculate the big oh of the binary search algorithm?

北城以北 提交于 2019-11-29 07:14:00
I'm looking for the mathematical proof, not just the answer. The recurrence relation of binary search is (in the worst case) T(n) = T(n/2) + O(1) Using Master's theorem n is the size of the problem. a is the number of subproblems in the recursion. n/b is the size of each subproblem. (Here it is assumed that all subproblems are essentially the same size.) f (n) is the cost of the work done outside the recursive calls, which includes the cost of dividing the problem and the cost of merging the solutions to the subproblems. Here a = 1, b = 2 and f(n) = O(1) [Constant] We have f(n) = O(1) = O(n

How many comparisons will binary search make in the worst case using this algorithm?

别说谁变了你拦得住时间么 提交于 2019-11-29 06:12:12
问题 Hi there below is the pseudo code for my binary search implementation: Input: (A[0...n-1], K) begin l ← 0; r ← n-1 while l ≤ r do m ← floor((l+r)/2) if K > A[m] then l ← m+1 else if K < A[m] then r ← m-1 else return m end if end while return -1 // key not found end I was just wondering how to calculate the number of comparisons this implementation would make in the worst case for a sorted array of size n? Would the number of comparisons = lg n + 1? or something different? 回答1: The worst-case

Python binary search-like function to find first number in sorted list greater than a specific value

二次信任 提交于 2019-11-29 05:14:36
I'm trying to write a function in Python that finds the first number in a sorted list greater than a specific value that I pass in as an argument. I've found examples online that use simple list comprehensions to achieve this, but for my purposes I need to be performing this operation frequently and on large lists, so a search that runs in linear time is too expensive. I've had a crack at writing an iterative binary search-like function to achieve this, though I'm coming across some edge cases where it doesn't work correctly. By the way, the function is not required to deal with a case where