alalbux / js-studies

0 stars 0 forks source link

Binary Search Tree #11

Open alalbux opened 5 years ago

alalbux commented 5 years ago
python
// linear search 
def linear_seach(input_array, value):
  index = 0
  while (index < len(input_array) and input_array[index] < value):
    index++;
  if (index >= len(input_array or input_array[index] != value):
    return -1
  return index

// binart search
def binary_search(input_array, value):
  low = 0
  high = len(input_array) -1
  while (low <= high) 
    mid = (low + high) / 2
    if (input_array[mid] == value)
      return mid
    elif (input_array[mid] < value)
      low = mid + 1
    else:
      high = -1
  return -1

test_list = [1,3,9,11,15,19,29]
test_val1 = 25
test_val2 = 15
print binary_search(test_list, test_val1)
print binary_search(test_list, test_val2)