-
Notifications
You must be signed in to change notification settings - Fork 96
/
recursiveBinarySearch.rb
58 lines (45 loc) · 1.48 KB
/
recursiveBinarySearch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def start_program #method defination
array_size = take_array_size_from_user
array = take_array_elements(array_size)
element = element_to_search
index_found_at = binary_search(array, element, 0, array_size - 1)
if index_found_at == -1
puts "Ughh... the element you want to search is not present in the given array"
else
puts "You Got it... the element is present in the given array at index #{index_found_at}"
end
end
def take_array_size_from_user
puts "Enter the size of the array"
array_size = gets.chomp.to_i
puts "Array size given by you is #{array_size}"
return array_size
end
def take_array_elements(array_size)
array = [] #initializing an empty array
array_size.times do |var| #looping array's size times to get that number of elements to fill in array
puts "Enter array element no. #{var}"
array << gets.chomp.to_i #takes the input in form of string and convert it to integer
end
return array
end
def element_to_search
puts "Enter element to search in array"
element = gets.chomp.to_i
return element
end
def binary_search(array, element, starts, ends)
if(starts > ends)
return -1
end
mid = starts + ((ends - starts)/2)
if(array[mid] == element)
return mid
elsif array[mid] > element
ends = mid - 1
elsif array[mid] < element
starts = mid + 1
end
binary_search(array, element, starts, ends)
end
start_program