-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexample_2.py
56 lines (51 loc) · 1.34 KB
/
example_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Binary Search
# JS version:
'''
var doSearch = function(array, targetValue) {
var minIndex = 0;
var maxIndex = array.length - 1;
var currentIndex;
var currentElement;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = array[currentIndex];
if (currentElement < targetValue) {
minIndex = currentIndex + 1;
} else if (currentElement > targetValue) {
maxIndex = currentIndex - 1;
} else {
return currentIndex;
}
}
return -1; //If the index of the element is not found.
};
'''
# Python version
def do_search(arr, target):
min_index = 0
max_index = len(arr) - 1
while min_index <= max_index:
curr_index = (min_index + max_index)//2
curr_value = arr[curr_index]
if curr_value == target:
return curr_index
elif curr_value < target:
min_index = curr_index + 1
elif curr_value > target:
max_index = curr_index - 1
return -1
numbers = [11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33]
print(do_search(numbers, 23)) # 6
#
#
#
#
#
#
#
#
#
#
# O(log n) time b/c is a binary search
# A logarithmic algorithm halves the input every time it’s run.
# O(1) space