Browse Source

Merge pull request #34 from jay4jyotika/master

added exponential searching algo
master
Jeffery Russell 5 years ago
committed by GitHub
parent
commit
8608d05876
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
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