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.

27 lines
610 B

  1. '''
  2. Sends array, searchValue, arraySize as input.
  3. Returns the index of the searchValue in the array
  4. '''
  5. def jumpSearch(arr , x , n ):
  6. # Finding block size to be jumped
  7. step = math.sqrt(n)
  8. # Finding the element
  9. prev = 0
  10. while arr[int(min(step, n)-1)] < x:
  11. prev = step
  12. step += math.sqrt(n)
  13. if prev >= n:
  14. return -1
  15. # Doing a linear search
  16. while arr[int(prev)] < x:
  17. prev += 1
  18. if prev == min(step, n):
  19. return -1
  20. if arr[int(prev)] == x:
  21. return prev
  22. return -1