binary_search2.rb
# Given sorted array A
# Find a key using binary search and get an index. 
# If A[index] == key
#     return index;
# else 
#    while(index > 1 && A[index] == A[index -1]) index = index -1;
#    return index;
Given sorted array A
Find a key using binary search and get an index.
Given sorted array A Find a key using binary search and get an index .
def iter_bindex(array, element)
	upper = array.size
	lower = 0
	while upper >= lower
		mid = (upper + lower) /2
		if array[mid] < element
			lower = mid + 1
		elsif array[mid] > element
			upper = mid -1
		else 
			return mid
		end
	end
	return nil
end
def iter_bindex ( array , element ) upper = array . size lower = 0 while upper >= lower mid = ( upper + lower ) / 2 if array [ mid ] < element lower = mid + 1 elsif array [ mid ] > element upper = mid - 1 else return mid end end return nil end
Given sorted array A
Find a key using binary search and get an index.
Given sorted array A Find a key using binary search and get an index .
array
<not_specific>
element
<not_specific>