From 4586b9dcd91c78f7a8dc56c8d0b58f74c0abad7e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:06:24 -0800 Subject: [PATCH 1/3] Add Search algorithm: Recursive linear search --- searches/recursive_linear_search.rb | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 searches/recursive_linear_search.rb diff --git a/searches/recursive_linear_search.rb b/searches/recursive_linear_search.rb new file mode 100644 index 0000000..67065cd --- /dev/null +++ b/searches/recursive_linear_search.rb @@ -0,0 +1,30 @@ +=begin +A pure Ruby implementation of a recursive linear search algorithm +=end + +def rec_linear_search(sequence, low, high, target) + unless (high < sequence.length && low < sequence.length) + raise Exception("Invalid upper or lower bound!") + end + + if high < low + return -1 + end + if sequence[low] == target + return low + end + if sequence[high] == target + return high + end + + return rec_linear_search(sequence, low + 1, high - 1, target) +end + +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)) +# => 0 +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)) +# => 4 +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)) +# => 1 +puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)) +# => -1 From c5898581f46a1309f80a3d42ee566e3688653d7d Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:08:48 -0800 Subject: [PATCH 2/3] Fix rubocop offenses --- searches/recursive_linear_search.rb | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/searches/recursive_linear_search.rb b/searches/recursive_linear_search.rb index 67065cd..0c80a8d 100644 --- a/searches/recursive_linear_search.rb +++ b/searches/recursive_linear_search.rb @@ -1,30 +1,25 @@ -=begin -A pure Ruby implementation of a recursive linear search algorithm -=end +# A pure Ruby implementation of a recursive linear search algorithm def rec_linear_search(sequence, low, high, target) - unless (high < sequence.length && low < sequence.length) - raise Exception("Invalid upper or lower bound!") - end + raise Exception('Invalid upper or lower bound!') unless high < sequence.length && low < sequence.length - if high < low - return -1 - end - if sequence[low] == target - return low - end - if sequence[high] == target - return high - end + return -1 if high < low - return rec_linear_search(sequence, low + 1, high - 1, target) + return low if sequence[low] == target + + return high if sequence[high] == target + + rec_linear_search(sequence, low + 1, high - 1, target) end puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0)) # => 0 + puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700)) # => 4 + puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30)) # => 1 + puts(rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6)) # => -1 From 99fae7a9309a35434f56848b9b7e215feb9f407e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 20 Dec 2020 16:11:46 -0800 Subject: [PATCH 3/3] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 48513cb..27bfeaa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -39,6 +39,7 @@ * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) + * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb)