Skip to content

Instantly share code, notes, and snippets.

@anil477
Created January 21, 2017 20:00
Show Gist options
  • Save anil477/abf3198ac6cfb1ff20ba697d64b92e86 to your computer and use it in GitHub Desktop.
Save anil477/abf3198ac6cfb1ff20ba697d64b92e86 to your computer and use it in GitHub Desktop.
Search for an element in a sorted rotated array without finding the pivot
class Search:
def __init__(self):
self.array = [3, 4, 5, 1, 2]
def search(self, low, high, key):
if low > high:
return -1
mid = (low + high) / 2
if self.array[mid] == key:
print " Found ", key, " at index ", mid
return
# sorted
if self.array[low] < self.array[mid]:
if self.array[low] <= key and key <= self.array[mid]:
return self.search(low, mid - 1, key)
return self.search(mid + 1, high, key)
else:
if self.array[mid] < key and key <= self.array[high]:
return self.search(mid + 1, high, key)
return self.search(low, mid - 1, key)
def start(self):
print self.search(0, len(self.array) - 1, 4)
s = Search()
s.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment