|
| 1 | +class SearchAlgorithms: |
| 2 | + |
| 3 | + def __init__(self): |
| 4 | + pass |
| 5 | + |
| 6 | + def linear(self, s_list, n): |
| 7 | + |
| 8 | + for i in range(len(s_list)): |
| 9 | + if s_list[i] == n: |
| 10 | + return i |
| 11 | + |
| 12 | + return -1 |
| 13 | + |
| 14 | + def binary(self, s_list, n, min_at, max_at): |
| 15 | + |
| 16 | + """ |
| 17 | + :param s_list: Sorted List which is to be searched from |
| 18 | + :param n: Element to be searched |
| 19 | + :param min_at: Zero |
| 20 | + :param max_at: Length of the list |
| 21 | + :return: Index at which the element was found / -1 If element absent |
| 22 | + """ |
| 23 | + |
| 24 | + mid = (min_at + max_at) // 2 |
| 25 | + if s_list[mid] == n: |
| 26 | + return mid |
| 27 | + elif s_list[mid] > n: |
| 28 | + return self.binary(s_list, n, min_at, mid - 1) |
| 29 | + else: |
| 30 | + return self.binary(s_list, n, mid + 1, max_at) |
| 31 | + |
| 32 | + return -1 |
| 33 | + |
| 34 | + def exponential(self, s_list, n, e): |
| 35 | + |
| 36 | + """ |
| 37 | + :param s_list: List to be searched from |
| 38 | + :param n: Element to be searched |
| 39 | + :param e: Raise function to the power e |
| 40 | + :return: Index at which the element was found / -1 If element absent |
| 41 | + """ |
| 42 | + |
| 43 | + if s_list[0] == n: |
| 44 | + return 0 |
| 45 | + |
| 46 | + at = 1 |
| 47 | + while at < len(s_list) and s_list[at] <= n: |
| 48 | + at = at * e |
| 49 | + |
| 50 | + if at > len(s_list): |
| 51 | + l = len(s_list) |
| 52 | + else: |
| 53 | + l = at |
| 54 | + |
| 55 | + return self.binary(s_list[:l], n, 0, len(s_list[:l])) |
| 56 | + |
| 57 | + def interpolation(self, s_list, n): |
| 58 | + |
| 59 | + low, high = 0, (len(s_list) - 1) |
| 60 | + while low <= high and s_list[low] <= n <= s_list[high]: |
| 61 | + try: |
| 62 | + at = low + int(((float(high - low) / (s_list[high] - s_list[low])) * (n - s_list[low]))) |
| 63 | + if s_list[at] == n: |
| 64 | + return at |
| 65 | + if s_list[at] < n: |
| 66 | + low = at + 1; |
| 67 | + else: |
| 68 | + high = at - 1; |
| 69 | + except ZeroDivisionError: |
| 70 | + break |
| 71 | + |
| 72 | + return -1 |
| 73 | + |
| 74 | + def hop(self, s_list, n, jump): |
| 75 | + |
| 76 | + """ |
| 77 | + :param s_list: List to be searched from |
| 78 | + :param n: Element to be searched |
| 79 | + :param jump: Make hops of how many elements? |
| 80 | + :return: Index at which the element was found / -1 If element absent |
| 81 | + """ |
| 82 | + low, high = 0, 0 |
| 83 | + l = len(s_list) |
| 84 | + |
| 85 | + while s_list[low] <= n and low < l: |
| 86 | + |
| 87 | + if l - 1 > low + jump: |
| 88 | + high = low + jump |
| 89 | + else: |
| 90 | + high = l - 1 |
| 91 | + |
| 92 | + if s_list[low] <= n <= s_list[high]: |
| 93 | + break |
| 94 | + |
| 95 | + low += jump; |
| 96 | + |
| 97 | + if low >= l or s_list[low] > n: |
| 98 | + return -1 |
| 99 | + |
| 100 | + if l - 1 > low + jump: |
| 101 | + pass |
| 102 | + else: |
| 103 | + high = l - 1 |
| 104 | + |
| 105 | + i = low |
| 106 | + |
| 107 | + while i <= high and s_list[i] <= n: |
| 108 | + |
| 109 | + if s_list[i] == n: |
| 110 | + return i |
| 111 | + |
| 112 | + i += 1 |
| 113 | + |
| 114 | + return -1 |
| 115 | + |
| 116 | + def fibonacci(self, s_list, n): |
| 117 | + |
| 118 | + neg_one, neg_two = 1, 0 |
| 119 | + fib = neg_one + neg_two |
| 120 | + |
| 121 | + while fib < len(s_list): |
| 122 | + neg_two, neg_one = neg_one, fib |
| 123 | + fib += neg_two |
| 124 | + |
| 125 | + at = -1 |
| 126 | + |
| 127 | + while fib > 1: |
| 128 | + i = min(at + neg_two, (len(s_list) - 1)) |
| 129 | + |
| 130 | + if s_list[i] < n: |
| 131 | + |
| 132 | + fib, neg_one = neg_one, neg_two |
| 133 | + neg_two, at = fib - neg_one, i |
| 134 | + |
| 135 | + elif s_list[i] > n: |
| 136 | + |
| 137 | + fib = neg_two |
| 138 | + neg_one -= neg_two |
| 139 | + neg_two = fib - neg_one |
| 140 | + |
| 141 | + else: |
| 142 | + return i |
| 143 | + |
| 144 | + if neg_one and at < (len(s_list) - 1) and s_list[at + 1] == n: |
| 145 | + return at + 1; |
| 146 | + return -1 |
0 commit comments