Skip to content

Commit 3cbd506

Browse files
authored
Merge pull request #3 from MsDiala/array-binary-search
Array binary search
2 parents cd1e574 + d7a3c3e commit 3cbd506

File tree

6 files changed

+92
-6
lines changed

6 files changed

+92
-6
lines changed

ALGO3.png

358 KB
Loading

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# data-structures-and-algorithms-python
2+
3+
24
# Challenge Summary
3-
IT was eady
5+
Write a function called BinarySearch which takes in 2 parameters: a sorted array and the search key. Without utilizing any of the built-in methods available to your language, return the index of the array’s element that is equal to the search key, or -1 if the element does not exist.
46
## Challenge Description
5-
I did a tests to a shift array
6-
## Approach & Efficiency
7-
TDD
8-
## Solution
9-
![Getting Started](dddddddddddd.png)
7+
wRITE A FUNCTION WHICH
8+
TAKES TWO PARAMS SORTED IN ARRAY AND SEARCH KEY AND RETURN INDEX OF ARR ELEMENT WHICH = TO SEARCH KEY OR "-1" IF IT'S NOT EXIST
9+
INPUT => [2,4,6,8],6
10+
OUTPUT => 2
11+
12+
## Solution![Getting Started](ALGO3.png)
13+
14+
15+
https://github.com/MsDiala/data-structures-and-algorithms-python/pull/3
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def binary_search(arr, key):
2+
low = 0
3+
high = len(arr)
4+
while len(arr) > 0:
5+
middle = (low+high) // 2
6+
if arr[middle] == key:
7+
return middle
8+
if arr[middle] < key:
9+
low= middle
10+
if arr[middle] > key:
11+
high= middle
12+
if middle == 0 or middle == len(arr) - 1:
13+
return -1
14+
return -1
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from data_structures_and_algorithms.challenges.array_reverse.array_search import binary_search
2+
3+
# "Happy Path" cases
4+
def test_simple():
5+
expected = 3
6+
actual = binary_search([1,2,3,4,5,6],4)
7+
assert actual == expected
8+
9+
def test_original_nums():
10+
expected = 2
11+
actual = binary_search([4,8,15,16,23,42], 15)
12+
assert actual == expected
13+
14+
def test_absence():
15+
expected = -1
16+
actual = binary_search([11,22,33,44,55,66,77], 90)
17+
assert actual == expected
18+
19+
# Expected Failure
20+
def test_num_in_left():
21+
expected = 0
22+
actual = binary_search([11,22,33,44,55,66,77,89,100], 11)
23+
assert actual == expected
24+
25+
def test_num_in_right():
26+
expected = 8
27+
actual = binary_search([11,22,33,44,55,66,77,89,100], 100)
28+
assert actual == expected
29+
30+
def test_num_in_middle():
31+
expected = 4
32+
actual = binary_search([11,22,33,44,55,66,77,89,100], 55)
33+
assert actual == expected
34+
35+
def test_num_not_in_right():
36+
expected = -1
37+
actual = binary_search([11,22,33,44,55,66,77,89,100], 101)
38+
assert actual == expected
39+
40+
def test_num_not_in_left():
41+
expected = -1
42+
actual = binary_search([11,22,33,44,55,66,77,89,100], 10)
43+
assert actual == expected
44+
45+
46+
47+
# Edge Cases
48+
def test_two_element_arr():
49+
expected = 1
50+
actual = binary_search([10, 34], 34)
51+
assert actual == expected
52+
53+
def test_two_element_arr_anbsence():
54+
expected = -1
55+
actual = binary_search([10, 34], 19)
56+
assert actual == expected
57+
58+
def test_empty_array():
59+
expected = -1
60+
actual = binary_search([], 19)
61+
assert actual == expected
62+
63+
def test_empty_array_negative():
64+
expected = -1
65+
actual = binary_search([], -1)
66+
assert actual == expected

0 commit comments

Comments
 (0)