Browse Source

added exponential searching algo

pull/34/head
Jyotika 5 years ago
parent
commit
b8de724277
1 changed files with 33 additions and 0 deletions
  1. +33
    -0
      searching_algo/exponentialSearch.py

+ 33
- 0
searching_algo/exponentialSearch.py View File

@ -0,0 +1,33 @@
def search(arr, l, r, x):
if r >= l:
m = (l+r)/2
if arr[m] == x:
return m
if arr[m] > x:
return search(arr, l, m-1, x)
else:
return search(arr, m, r, x)
return -1
def exponentialSearch(arr, n, x):
if arr[0] == x:
return 0
i = 1
while i<n and arr[i] <=x:
i = i*2
return search(arr, i/2, min(n, i), x)
arr = [2, 3, 4, 7, 10, 40]
n = len(arr)
x = 10
r = exponentialSearch(arr, n, x)
if r == -1:
print "Element not found"
else:
print "Element is present at index %d" %(r)

Loading…
Cancel
Save