Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#binarySearch(array, key) ⇒ Object



23
24
25
# File 'lib/algoritmos.rb', line 23

def binarySearch(array, key) 
  split_array(array,key,0,array.length - 1)
end

#split_array(array, key, start_index, end_index) ⇒ Object

Primeiro passo Retornar a metade de um array



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/algoritmos.rb', line 7

def split_array(array, key, start_index, end_index)
  # Receber um array, Dividi-lo e retornar a parte conveniente

  return -1 if start_index > end_index
  #sizeArray = array.length # Pegar o comprimento total
  #halfIndex = (sizeArray / 2) - 1 # Temos aqui o índice do elemento do meio
  half_index = (start_index + end_index) / 2
  if array[half_index] == key
    return half_index
  elsif key < array[half_index]
    split_array(array, key, start_index, half_index - 1)
  else
    split_array(array, key, half_index + 1, end_index)
  end
end