Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Kalpak Take authored Feb 1, 2017
1 parent 05799bf commit eaaf2f8
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions binary_search_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


def binary_search(array, n):
arr = sorted(array)
to_return = False
first_elem = 0
last_elem = len(arr) - 1
while (first_elem <= last_elem):
mid = (first_elem + last_elem) // 2
if (arr[mid] == n):
to_return = True
break
else:
if (n > arr[mid]):
first_elem = mid + 1
else:
last_elem = mid - 1
return to_return

nums = [0,23,54,5,32,78]
print binary_search(nums, 32)
print binary_search(nums, 5)
15 changes: 15 additions & 0 deletions linear_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Linear Search or Sequential Search Algorithm

def linear_search(array, to_find):
pos = 0 # Starting position or index
to_return = (False, 0)
while (pos < len(array)): # while index is less than length of array
if (array[pos] == to_find): # if array with index of var pos is equal to find
to_return = (True, pos) # no need to break loop cuz return appends func
return to_return
else:
pos = pos + 1 # if elem not found continue to next pos
return to_return

nums = [12, 34, 54, 88, 21]
print linear_search(nums, 88)

0 comments on commit eaaf2f8

Please sign in to comment.