Recursive Double Linear Search

# Iterate through the array to find the index of key using recursion.

def recursive_double_linear_search(data, key, left = 0, right = 0)
  right &&= data.length - 1

  return -1 if left > right

  return left if data[left] == key
  return right if data[right] == key

  recursive_double_linear_search(data, key, left + 1, right - 1)
end

puts(recursive_double_linear_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 5))
# => 5

puts(recursive_double_linear_search([1, 2, 4, 5, 3], 4))
# => 2

puts(recursive_double_linear_search([1, 2, 4, 5, 3], 6))
# => -1

puts(recursive_double_linear_search([5], 5))
# => 0

puts(recursive_double_linear_search([], 1))
# => -1
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.