Answers for "binary search in unsorted list in python"

0

binary search in unsorted list in python

#this is example of binary search in unsorted list using "in" operator so you can skip sorting the list
def binarysearch_in_unsortedList(arr : List,item : int) -> int:
  
  Found = False
  low = 0
  high = len(arr)-1
  while not Found and low <= high:
    mid = (low + high) // 2
    if arr[mid] == item:
      Found = True
      print('{} at index {} in {}'.format(item,mid,arr))
      return mid
    else:
      if item in arr[mid: ]:
        low = mid + 1
      elif item in arr[0:mid-1]:
        high = mid - 1
      else:
        return -1
Posted by: Guest on May-02-2021

Python Answers by Framework

Browse Popular Code Answers by Language