-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from tinyCodersDen/main
Added Interpolation Search Program using Python
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Python Implementation for Interpolation Search Algorithm: | ||
|
||
def interpolationSearch(arr, lo, hi, x): | ||
|
||
# The target must be in range defined by the corner: | ||
if (lo <= hi and x >= arr[lo] and x <= arr[hi]): | ||
|
||
# Probing position(uniform distance): | ||
pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) * (x - arr[lo])) | ||
|
||
# Target found: | ||
if arr[pos] == x: | ||
return pos | ||
|
||
# If target is larger, it's in right subarray: | ||
if arr[pos] < x: | ||
return interpolationSearch(arr, pos + 1,hi, x) | ||
|
||
# If target is smaller, it's in left subarray: | ||
if arr[pos] > x: | ||
return interpolationSearch(arr, lo, pos - 1, x) | ||
return -1 | ||
|
||
# Array of sorted items: | ||
arr = [10, 21, 35, 47, 52, 58, 76, 81, 92, 96] | ||
|
||
# Element to be searched | ||
target = 35 | ||
index = interpolationSearch(arr, 0, len(arr) - 1, target) | ||
|
||
# Checking if the element is found or not: | ||
if index != -1: | ||
print("Element found at index", index) | ||
else: | ||
print("Element not found") |