Skip to content

Commit e3928c4

Browse files
committed
Searching
1 parent abbde8c commit e3928c4

6 files changed

+95
-0
lines changed

searching/binary-search-iterative.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
def binary_search(nums: list, target: int) -> bool:
3+
start: int = 0
4+
end: int = len(nums) - 1
5+
found: bool = False
6+
while start <= end and not found:
7+
midpoint: int = start + (end - start) // 2
8+
if nums[midpoint] == target:
9+
found = True
10+
else:
11+
if nums[midpoint] > target:
12+
end = midpoint - 1
13+
else:
14+
start = midpoint + 1
15+
return found
16+
17+
18+
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42]
19+
print(binary_search(testlist, 3))
20+
print(binary_search(testlist, 13))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
def binary_search(nums: list, target: int) -> bool:
3+
4+
def binary_search_helper(numbers: list, element, start: int, end: int) -> bool:
5+
if start > end:
6+
return False
7+
else:
8+
midpoint: int = start + (end - start) // 2
9+
if nums[midpoint] == element:
10+
return True
11+
else:
12+
if nums[midpoint] > element:
13+
return binary_search_helper(numbers, element, start, midpoint - 1)
14+
else:
15+
return binary_search_helper(numbers, element, midpoint + 1, end)
16+
17+
return binary_search_helper(nums, target, 0, len(nums) - 1)
18+
19+
20+
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42]
21+
print(binary_search(testlist, 3))
22+
print(binary_search(testlist, 13))

searching/binary-search-recursive.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# slicing a list is O(k)
2+
# better is recursive solution with pointers
3+
def binary_search(nums: list, target: int) -> bool:
4+
if len(nums) == 0:
5+
return False
6+
else:
7+
midpoint: int = len(nums) // 2
8+
if nums[midpoint] == target:
9+
return True
10+
else:
11+
if nums[midpoint] > target:
12+
return binary_search(nums[0: midpoint], target)
13+
else:
14+
return binary_search(nums[midpoint + 1:], target)
15+
16+
17+
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42]
18+
print(binary_search(testlist, 3))
19+
print(binary_search(testlist, 13))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
def sequential_search(nums: list, target: int) -> bool:
3+
found: bool = False
4+
stopped: bool = False
5+
i: int = 0
6+
while i < len(nums) and not found and not stopped:
7+
if nums[i] == target:
8+
found = True
9+
else:
10+
if nums[i] > target:
11+
stopped = True
12+
else:
13+
i += 1
14+
return found
15+
16+
17+
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42]
18+
print(sequential_search(testlist, 3))
19+
print(sequential_search(testlist, 13))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
def sequential_search(nums: list, target: int) -> bool:
3+
i: int = 0
4+
found: bool = False
5+
while i < len(nums) and not found:
6+
if nums[i] == target:
7+
found = True
8+
else:
9+
i += 1
10+
return found
11+
12+
13+
testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0]
14+
print(sequential_search(testlist, 3))
15+
print(sequential_search(testlist, 13))

sorting/bubble-sort.py

Whitespace-only changes.

0 commit comments

Comments
 (0)