Advertisement
kenadams53

Search_bisection_recursion

Sep 1st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. # 4.8 (102) Search_bisection_recursion Pythom code by Ken Adams
  2. # Finding a number from a sorted list by bisection and recursion
  3. # test data all these items on a list [7, 11, 25, 31, 43, 53, 64, 71, 85, 92]
  4. # additional test data these items not on the list 3,8,18,27,41,51,60,68,75,89 was used to verify no item present
  5. # In above we choose a less than the bottom of the list, a number more han the top of the list, and numbers between
  6. # any two numbers on the list
  7.  
  8. #PICK YOUR TEST DATA
  9. my_this = [7, 11, 25, 31, 43, 53, 64, 71, 85, 92]# used for testing
  10. #my_this = [7, 11, 25, 25, 25, 25, 64, 71, 85, 92]# used to test finding the lowest index of a number in the list
  11. #my_this = [25, 25, 25, 25, 25, 25, 64, 71, 85, 92]# same as above
  12. print(my_list)
  13.  
  14. my_number = 25 # set the number to search for
  15.  
  16. # note: we need four parameters
  17. def search(lower, upper, S_list, num):# this is the basic bisection algorithm
  18. print("lower " + str(lower) + " upper " +str(upper)) # lower and upper are indexes in the list
  19. mid = (lower+upper)//2 # roughly the mid point
  20. if mid == lower: # This is the test that stops recursion, whe mid = lower there can be no more bisections
  21. if S_list[mid] == num: # first check if mid is the correct answer
  22. return mid
  23. else:
  24. print(" number not on list ")# if mid is not the answer then the number is not on the list
  25. return None
  26. # At this point we have decided that there is at least one more recursion
  27. if S_list[mid] == num: # if mid correct then return it
  28. return mid
  29. if S_list[mid] > num: # we do another recursion on the lower part of the list
  30. return search(lower, mid, S_list, num)
  31. if S_list[mid] < num: # we do another recursion on the upper part of the list
  32. return search(mid, upper, S_list, num)
  33. # all posibilities have been exausted
  34.  
  35. # this little function lets us get away with two parameters
  36. def run_search(my_list, my_number):
  37. return search(0,len(my_list), my_list, my_number)
  38. # thats all folks
  39.  
  40. # this function, if it finds the requird number, will return the lowest index where it appears
  41. def run_search_smallest(my_list, my_number):
  42. first = search(0,len(my_list), my_list, my_number)# get one place to start
  43. while my_list[first] == my_number and first >= 0:
  44. store_first = first #store the result of the last loop throught the while
  45. first -= 1 # decrement first
  46. return store_first
  47. # note: the last time through the loop first has already been decremented so is one index too small
  48. # but store_first holds the answer we want
  49. # should store_first = 0, then first is decremented to -1,
  50. # the 'and' part of the while logic prevents this causing trouble
  51.  
  52. #CHOOSE WHAT FUNCTION TO RUN
  53. #position = search(0,len(my_list), this_list, my_number)# works with four parameters
  54. position = run_search(this_list, my_number)# works with two parameters
  55. position = run_search_smallest(this_list, my_number)# find the first correct index
  56.  
  57. # prints results
  58. print(str(my_number) + " is at position " + str(position))
  59.  
  60.  
  61. print("bye from Ken")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement