Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
517 B

  1. def search(arr, l, r, x):
  2. if r >= l:
  3. m = (l+r)/2
  4. if arr[m] == x:
  5. return m
  6. if arr[m] > x:
  7. return search(arr, l, m-1, x)
  8. else:
  9. return search(arr, m, r, x)
  10. return -1
  11. def exponentialSearch(arr, n, x):
  12. if arr[0] == x:
  13. return 0
  14. i = 1
  15. while i<n and arr[i] <=x:
  16. i = i*2
  17. return search(arr, i/2, min(n, i), x)
  18. arr = [2, 3, 4, 7, 10, 40]
  19. n = len(arr)
  20. x = 10
  21. r = exponentialSearch(arr, n, x)
  22. if r == -1:
  23. print "Element not found"
  24. else:
  25. print "Element is present at index %d" %(r)