Created
October 10, 2020 13:06
-
-
Save les-peters/98061ee222617b44b91ffa1367b4528a to your computer and use it in GitHub Desktop.
Cassidoo 2020-10-05
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
question = """ | |
Given an array that was once sorted in ascending order is rotated at some | |
pivot unknown to you beforehand (so [0,2,4,7,9] might become [7,9,0,2,4], for | |
example). Find the minimum value in that array in O(n) or less. | |
""" | |
def find_pivot_point(a): | |
start = 0 | |
end = len(a) - 1 | |
mid = int((start + end) / 2) | |
O_n = 0 | |
while True: | |
if start == mid or mid == end: | |
O_n += 1 | |
return(end if a[start] > a[end] else start, O_n) | |
else: | |
if a[start] > a[mid]: | |
O_n += 1 | |
end = mid | |
elif a[mid] > a[end]: | |
O_n += 1 | |
start = mid | |
mid = int((start + end) / 2) | |
a = [ 7, 9, 0, 2, 4 ] | |
pivot_point, O_n = find_pivot_point(a) | |
print('minimum value = {} at position {}'.format(a[pivot_point], pivot_point)) | |
print('n = ' + str(len(a)) + '; O(n) = ' + str(O_n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment