Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. sortList = [1,4,4,5,6,7,8,8.5,8.6,9,11,133]
  2. value = 1
  3. def binSearch(value,myList):
  4. left = 0
  5. right = len(myList) - 1
  6. found = False
  7. mid = int((left + right)/2)
  8. while value != myList[mid]:
  9. mid = int((left + right)/2)
  10. if myList[mid] > value:
  11. right = mid
  12. elif myList[mid] < value:
  13. left = mid
  14. mid = int((left + right)/2)
  15. if left == mid:
  16. found=False
  17. pnt1 = value - myList[mid]
  18. pnt2 = myList[mid+1] - value
  19. if pnt2 < pnt1:
  20. mid += 1
  21. return mid,found
  22. elif value == myList[mid]:
  23. while value == myList[mid+1]:
  24. mid += 1
  25. found = True
  26. found = True
  27. return mid,found
  28. answer,found = binSearch(value,sortList)
  29. if found == True:
  30. print("Value found at " + str(answer))
  31. if found == False:
  32. print("Nearest value at " + str(answer))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement