Advertisement
kenadams53

binary_search_code

Sep 1st, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. # 4.8 (102) python bisection search code by Raspary pi (Furure Learn) modified by Ken Adams
  2. #Uses the function 'binary_search' binary method to search a list for a given value.
  3. #If the value is not there it can return False or return the nearest item on the list
  4. # Unfortunately with the code at present (x integer) x.5 would get round down to x instead of up to x+1
  5. # Another function 'run_binary_search_smallest' will return the lowest index available for an item on the list.
  6.  
  7. #my_list=[1, 2, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8, 9]
  8. my_list = [1,2,3,5,6,7]
  9. my_value = 3
  10.  
  11. def binary_search(sorted_list, value,near):# near is set to 1 if you cant get exact but will accept the nearest
  12. # left to 0
  13. left = 0
  14. # right to highest index in list
  15. right = len(sorted_list) - 1
  16. # loop that ends when left > right
  17. while left <= right:
  18. # mid to int between left and right
  19. mid = int((right + left)/2)
  20. # if sorted_list[mid] > value set right to mid
  21. if sorted_list[mid] > value:
  22. right = mid - 1
  23. # if sorted_list[mid] < value set left to mid
  24. elif sorted_list[mid] < value:
  25. left = mid + 1
  26. # if sorted_list[mid] == value return mid
  27. else:
  28. return mid
  29. # loop ends return `False`
  30.  
  31. ###############################looking for the nearst
  32. if near == 1:
  33. print("finding the closest")
  34. if mid == 0: # have searched to the botom of the list so value must be below the bottom
  35. return 0
  36. elif mid == len(sorted_list)-1: # have searched to top of list go value beyoud that
  37. return mid
  38. else:
  39. closest = mid-1 #any of the three mid-1, mid, mid+1 could fetch the closest
  40. # we start at closest = mid-1 and use a for loop to change that if nessary
  41. for i in range(2):
  42. if abs(sorted_list[closest]-value) > abs(sorted_list[closest+1]-value):
  43. closest = closest +1
  44. return closest
  45. return False # f near was not set to one the if above is not used so we return False as usual
  46.  
  47. # function to find lowest
  48. def run_binary_search_smallest(my_list, my_number): #uses their binary_search not my own
  49. first = binary_search(my_list, my_value,)# get one place to start
  50. while my_list[first] == my_number and first >= 0:
  51. store_first = first #store the result of the last loop throught the while
  52. first -= 1 # decrement first
  53. return store_first
  54. # CHOOSE WHAT FUNCTION TO RUN
  55. position = binary_search(my_list, my_value,1)
  56. #position = run_binary_search_smallest(my_list, my_value)
  57. print(position)
  58. print("Looking for " + str(my_value) + " The item at position " + str(position) + " is " +str(my_list[position]))
  59.  
  60. print("bye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement