Skip to content

Commit d559207

Browse files
authored
Added Binary_Search.coffee (jainaman224#66)
1 parent 0e26ef9 commit d559207

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

Binary_Search/Binary_Search.coffee

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Binary_Search = (array, desired) ->
2+
left = 0
3+
right = array.length - 1
4+
5+
while left <= right
6+
middle = left + (right - left) / 2
7+
8+
return middle if array[middle] is desired
9+
10+
if desired < array[middle]
11+
right = middle - 1
12+
else
13+
left = middle + 1
14+
15+
-1
16+
17+
18+
array = [1, 2, 3, 4, 5, 6, 7]
19+
20+
index = Binary_Search array, 4
21+
22+
if index isnt -1
23+
console.log "Found"
24+
else
25+
console.log "Not Found"
26+
27+
index = Binary_Search array, 9
28+
29+
if index isnt -1
30+
console.log "Found"
31+
else
32+
console.log "Not Found"
33+
34+
35+
### Output
36+
37+
Found
38+
Not Found
39+
40+
###

Binary_Search/Binary_Search.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
<?php
21
$list = array(3, 4, 25, 45, 78);
32
$element_to_be_found = 25;
43

54
function binary_search($element_to_be_found, $list, $left, $right)
65
{
76
if ($left > $right)
87
return -1;
9-
8+
109
$mid = ($right + $left) / 2;
11-
10+
1211
if($list[$mid] == $element_to_be_found)
1312
return $mid;
1413
else if($list[$mid] > $element_to_be_found)
@@ -23,5 +22,3 @@ function binary_search($element_to_be_found, $list, $left, $right)
2322
echo "Element not found";
2423
else
2524
echo "Element found at index $ans";
26-
27-
?>

Binary_Search/Binary_Search.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def Binary_Search(array, desired):
3030
else:
3131
print("Not Found")
3232

33+
3334
''' Output
3435
3536
Found

Binary_Search/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ end procedure
6868

6969
## Implementation
7070

71+
- [CoffeeScript Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Binary_Search/Binary_Search.coffee)
72+
> :rocket: [Compile Online](https://repl.it/C9ze) :rocket:
73+
7174
- [C++ Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Binary_Search/Binary_Search.cpp)
7275
> :rocket: [Compile Online](https://repl.it/C6n3) :rocket:
7376
7477
- [Java Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Binary_Search/Binary_Search.java)
7578
> :rocket: [Compile Online](https://repl.it/C6n1) :rocket:
7679
80+
- [Php Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Binary_Search/Binary_Search.php)
81+
> :rocket: [Compile Online](https://repl.it/C9zo) :rocket:
82+
7783
- [Python Code](https://github.com/jainaman224/Algo_Ds_Notes/blob/master/Binary_Search/Binary_Search.py)
7884
> :rocket: [Compile Online](https://repl.it/C6nZ) :rocket:

0 commit comments

Comments
 (0)