Guest User

Untitled

a guest
Jan 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. from __future__ import print_function
  2. from __future__ import division
  3. import math
  4.  
  5. #the array input needs to be already sorted
  6. def binary_search(array, start, end, key):
  7. if end >= 1:
  8. mid = start + (end-start)//2
  9. if array[mid] is key:
  10. return mid
  11. elif array[mid] > key:
  12. return binary_search(array, start, mid-1, key)
  13. # binary search the second half
  14. elif array[mid] < key:
  15. return binary_search(array, mid+1, end, key)
  16. return -1 #point where key is not present in the array anymore
  17.  
  18.  
  19. '''Knuth's algorithm for uniform binary search:
  20. based on the idea that instead of having an upper bound,
  21. a lower bound and a midpoint, we use two ptrs: current
  22. position i and one for delta (rate of change)'''
  23. def uniform_binary_search(array, i, delta, key):
  24. i = math.ceil(array.size()/2) #current position
  25. m = math.floor(array.size()/2) #num of current records
  26. if (key == array[i]):
  27. return i
  28. elif m is not 0:
  29. if array[i] > key:
  30. i = i - math.ceil(m/2)
  31. m = math.floor(m/2)
  32. return uniform_binary_search(array, i, m, key)
  33. else:
  34. i = i + math.ceil(m/2)
  35. m = math.floor(m/2)
  36. return uniform_binary_search(array, i, m, key)
  37.  
  38. # but don't need to maintain the value of m at all, we
  39. # only need to refer to a short table of the various delta
  40. # to use at each evel of the tree, alg is reduced to a lookup table
  41. def delta_table(num, j):
  42. delta = []
  43. if j >= 1 and j <= math.floor(math.log(num[2])) + 2:
  44. delta[j] = math.floor(num + math.pow(2, j-1))
  45. j = j + 1
  46. return delta[j-1]
  47. return -1 #error
  48.  
  49. def uniform_binary_search_delta(array, i, delta, key):
  50. i = delta_table(array.size(), 0) - 1 #midpoint of array
  51. d = 0
  52. if key == array[i]:
  53. return i
  54. elif delta_table(array.size(), d) == 0:
  55. return -1
  56. else:
  57. if array[i] > key:
  58. d = d + 1
  59. i = i - delta_table(array.size(), d)
  60. elif array[i] < key:
  61. d = d + 1
  62. i = i + delta_table(array.size(), d)
  63.  
  64. def factorial(n): #this is better in dynamic programming
  65. if n == 1:
  66. return 1
  67. elif n > 1:
  68. return n*factorial(n-1)
  69. return -1
  70.  
  71. def palidrome(word, start, end):
  72. length = word.length()
  73. if length == 0 or length == 1:
  74. return True
  75. if word[0] == word[length]:
  76. return True
  77. return palidrome(word, start+1, end-1)
  78.  
  79. '''
  80. If n is even, then x^n = (x^2)^(n/2)
  81. If n is odd, then x^n = x * (x^2)^((n-1)/2)
  82. '''
  83. def powers_of_number(base, exponent):
  84. if exponent == 0:
  85. return 1
  86. #return powers_of_number(base, exponent-1) * base #this takes too much time if the base and exponent are large
  87. elif exponent%2 == 0 and exponent > 0:
  88. print("even")
  89. return powers_of_number(base*base, exponent/2)
  90. elif exponent%2 == 1 and exponent > 0:
  91. print("odd")
  92. return powers_of_number(base*base, (exponent-1)/2) * base
  93. elif exponent < 0:
  94. return powers_of_number(base, exponent+1)/base
  95.  
  96.  
  97. def insertion_sort(array):
  98. done = False
  99. i = 1
  100. while not done:
  101. j = i
  102. temp = array[i]
  103. while temp < array[j-1] and j > 0:
  104. array[j] = array[j-1]
  105. j = j - 1
  106. array[j] = temp #put it into position
  107. i = i + 1
  108. if i >= len(array) or j >= len(array):
  109. done = True
  110.  
  111. def merge_sort(array):
  112. if len(array) > 1:
  113. mid = len(array)//2
  114. left_half = array[ :mid]
  115. right_half = array[mid: ]
  116. merge_sort(left_half)
  117. merge_sort(right_half)
  118.  
  119. left_index = 0
  120. right_index = 0
  121. k = 0
  122. while left_index < len(left_half) and right_index < len(right_half):
  123. if left_half[left_index] < right_half[right_index]:
  124. array[k] = left_half[left_index]
  125. left_index = left_index + 1
  126. else:
  127. array[k] = right_half[right_index]
  128. right_index = right_index + 1
  129. k = k + 1
  130. #exhaused arrays:
  131. #if left_index == len(left_half)-1:
  132. while right_index < len(right_half):
  133. array[k] = right_half[right_index]
  134. right_index = right_index + 1
  135. k = k + 1
  136.  
  137. #if right_index == len(right_half)-1:
  138. while left_index < len(left_half):
  139. array[k] = left_half[left_index]
  140. left_index = left_index + 1
  141. k = k + 1
  142.  
  143. '''
  144. uses left and right pointers to array elm to find correct partitioning
  145. locations separating partition index value
  146. '''
  147. def swap(array, left, right):
  148. temp = array[left]
  149. array[left] = array[right]
  150. array[right] = temp
  151.  
  152. def partition(array, left, right):
  153. mid = left + (right-left)//2
  154. # make pivot the of array and swap it with leftmost elm
  155. pivot = array[mid]
  156. array[mid] = array[left]
  157. array[left] = pivot
  158. print("pivot in partition: ", pivot)
  159. forward = left + 1
  160. backward = right
  161. done = False
  162. # while forward <= backward: doesn't work
  163. while not done:
  164. while forward <= backward and array[forward] <= pivot:
  165. forward = forward + 1
  166. while array[backward] >= pivot and backward >= forward:
  167. backward = backward - 1
  168. # should stop at this point when they are improperly placed
  169. # array[forward] > pivot and when array[backward] < pivot
  170. if backward < forward:
  171. done = True #exit out of loop
  172. #if forward < backward:
  173. else:
  174. swap(array, forward, backward)
  175.  
  176. # reach until they cross when backward < forward
  177. # swap backward and first to put mid back in partition position
  178. swap(array, left, backward)
  179. print ("array backward: ", backward)
  180. # return backward index
  181. return backward
  182.  
  183. def quicksort(array, front, back):
  184. if front < back:
  185. pivot_position = partition(array, front, back)
  186. quicksort(array, front, pivot_position-1)
  187. quicksort(array, pivot_position+1, back)
  188.  
  189. if __name__ == '__main__':
  190. array = [54,26,93,17,77,31,44,55,20]
  191. print ("Original array:", array)
  192. quicksort(array, 0, len(array) - 1)
  193. print ("After quicksort: ", array)
  194. #insertion_sort(array)
  195. #merge_sort(array)
  196. #print(array)
  197. #print (powers_of_number(2, -1))
  198. print (binary_search(array, 0, len(array)-1, 31))
Add Comment
Please, Sign In to add comment