Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit eaaf2f8

Browse files
author
Kalpak Take
authored
Add files via upload
1 parent 05799bf commit eaaf2f8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

binary_search_algorithm.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
def binary_search(array, n):
4+
arr = sorted(array)
5+
to_return = False
6+
first_elem = 0
7+
last_elem = len(arr) - 1
8+
while (first_elem <= last_elem):
9+
mid = (first_elem + last_elem) // 2
10+
if (arr[mid] == n):
11+
to_return = True
12+
break
13+
else:
14+
if (n > arr[mid]):
15+
first_elem = mid + 1
16+
else:
17+
last_elem = mid - 1
18+
return to_return
19+
20+
nums = [0,23,54,5,32,78]
21+
print binary_search(nums, 32)
22+
print binary_search(nums, 5)

linear_search.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Linear Search or Sequential Search Algorithm
2+
3+
def linear_search(array, to_find):
4+
pos = 0 # Starting position or index
5+
to_return = (False, 0)
6+
while (pos < len(array)): # while index is less than length of array
7+
if (array[pos] == to_find): # if array with index of var pos is equal to find
8+
to_return = (True, pos) # no need to break loop cuz return appends func
9+
return to_return
10+
else:
11+
pos = pos + 1 # if elem not found continue to next pos
12+
return to_return
13+
14+
nums = [12, 34, 54, 88, 21]
15+
print linear_search(nums, 88)

0 commit comments

Comments
 (0)