binary search in unsorted list in python

Solutions on MaxInterview for binary search in unsorted list in python by the best coders in the world

showing results for - "binary search in unsorted list in python"
Soumaya
03 Jun 2018
1#this is example of binary search in unsorted list using "in" operator so you can skip sorting the list
2def binarysearch_in_unsortedList(arr : List,item : int) -> int:
3  
4  Found = False
5  low = 0
6  high = len(arr)-1
7  while not Found and low <= high:
8    mid = (low + high) // 2
9    if arr[mid] == item:
10      Found = True
11      print('{} at index {} in {}'.format(item,mid,arr))
12      return mid
13    else:
14      if item in arr[mid: ]:
15        low = mid + 1
16      elif item in arr[0:mid-1]:
17        high = mid - 1
18      else:
19        return -1