chore(Python): added interpolation search file (#334)
parent
5b028ab3c3
commit
cd476b055e
|
@ -9,19 +9,20 @@
|
||||||
## Linked Lists
|
## Linked Lists
|
||||||
1. [Doubly](linked_lists/doubly.py)
|
1. [Doubly](linked_lists/doubly.py)
|
||||||
2. [Singly](linked_lists/singly.py)
|
2. [Singly](linked_lists/singly.py)
|
||||||
|
|
||||||
## Multiplication
|
## Multiplication
|
||||||
1. [Karatsuba](multiplication/karatsuba.py)
|
1. [Karatsuba](multiplication/karatsuba.py)
|
||||||
|
|
||||||
## Scheduling
|
## Scheduling
|
||||||
1. [Interval Scheduling](scheduling/interval_scheduling.py)
|
1. [Interval Scheduling](scheduling/interval_scheduling.py)
|
||||||
|
|
||||||
## Searching
|
## Searching
|
||||||
1. [Binary Search](searching/binary_search.py)
|
1. [Binary Search](searching/binary_search.py)
|
||||||
2. [Jump Search](searching/jump_search.py)
|
2. [Jump Search](searching/jump_search.py)
|
||||||
3. [Linear Search](searching/linear_search.py)
|
3. [Linear Search](searching/linear_search.py)
|
||||||
4. [Ternary Search](searching/ternary_search.py)
|
4. [Ternary Search](searching/ternary_search.py)
|
||||||
|
5. [Interpolation Search](searching/interpolation_search.py)
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
1. [Bubble Sort](sorting/bubble_sort.py)
|
1. [Bubble Sort](sorting/bubble_sort.py)
|
||||||
2. [Comb Sort](sorting/comb_sort.py)
|
2. [Comb Sort](sorting/comb_sort.py)
|
||||||
|
@ -31,7 +32,7 @@
|
||||||
6. [Heap Sort](sorting/heap-sort.py)
|
6. [Heap Sort](sorting/heap-sort.py)
|
||||||
7. [Radix Sort](sorting/radix-sort.py)
|
7. [Radix Sort](sorting/radix-sort.py)
|
||||||
8. [Shell Sort](sorting/shell-sort.py)
|
8. [Shell Sort](sorting/shell-sort.py)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
1. [Is Good Str](strings/is_good_str.py)
|
1. [Is Good Str](strings/is_good_str.py)
|
||||||
2. [Palindrome](strings/palindrome.py)
|
2. [Palindrome](strings/palindrome.py)
|
||||||
|
@ -44,4 +45,3 @@
|
||||||
1. [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
1. [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
||||||
2. [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py )
|
2. [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py )
|
||||||
3. [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
3. [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
def interpolation_search(array, x):
|
||||||
|
"""
|
||||||
|
Use interpolation search to find a specified integer
|
||||||
|
"""
|
||||||
|
if isinstance(x, int) and isinstance(array, list): # check that x is integer and array is list
|
||||||
|
if len(array) < 1: # if array length is less than 1 print message
|
||||||
|
return False
|
||||||
|
elif len(array) == 1 and x in array: # if x is only element of array return index 0
|
||||||
|
return 0
|
||||||
|
elif len(array) > 1 and x in array: # search if array length > 1 and x is in the array
|
||||||
|
min = 0
|
||||||
|
max = len(array) - 1
|
||||||
|
while min <= max:
|
||||||
|
if array[min] == array[max] == x:
|
||||||
|
return min
|
||||||
|
elif array[min] == array[max] != x:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
p = int(min + (x-array[min])*(max-min) / (array[max]-array[min]))
|
||||||
|
if array[p] == x:
|
||||||
|
return p
|
||||||
|
elif x < array[p]:
|
||||||
|
max = p - 1
|
||||||
|
continue
|
||||||
|
elif x > array[p]:
|
||||||
|
min = p + 1
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
return False # if x is not in array return false
|
||||||
|
else:
|
||||||
|
raise Exception('Please make sure x is an integer and array is a list.')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
tests = [
|
||||||
|
([1,4,7,10,13,16], 13),
|
||||||
|
([1,4,7,10,13,16], 11),
|
||||||
|
([1,1,1,1,1,1], 1),
|
||||||
|
([0,0,0,1,1,1,1,1,1,2,3,4,5], 1),
|
||||||
|
([24], 24),
|
||||||
|
([], 24)
|
||||||
|
]
|
||||||
|
|
||||||
|
for test in tests:
|
||||||
|
print(interpolation_search(test[0], test[1]))
|
Loading…
Reference in New Issue