-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kalpak Take
authored
Feb 1, 2017
1 parent
05799bf
commit eaaf2f8
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |