Skip to content

Commit 421199f

Browse files
committed
exercism-64 Binary-Search
1 parent 50da5ee commit 421199f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
281281
61. [Bob](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/bob.rb)
282282
62. [Nth Prime](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/nth_prime.rb)
283283
63. [Perfect Numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/perfect_numbers.rb)
284+
64. [Binary Search](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/binary_search.rb)
284285

285286
<a name="leetcode"/>
286287

exercism/binary_search.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/binary-search
2+
3+
# Solution
4+
class BinarySearch
5+
def initialize(playlist)
6+
@playlist = playlist
7+
end
8+
9+
def search_for(song,left=0,right=@playlist.length-1)
10+
return nil if left>right
11+
mid = left+((right-left)/2)
12+
return mid if @playlist[mid]==song
13+
if @playlist[mid]>song
14+
return search_for(song,left,mid-1)
15+
else
16+
return search_for(song,mid+1,right)
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)