diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-11-25 20:38:03 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-11-25 20:38:03 +0200 |
commit | 4531afb7137e85dbdc45ec9147612a101568a507 (patch) | |
tree | 473fcf5b2e0ab7b1852ca7749b91231d797e2e73 /algorithms/binary_sort.py | |
parent | 5df83b0e15a757b4803b66b9af6a7e5afcd1667e (diff) | |
download | cs-y13-4531afb7137e85dbdc45ec9147612a101568a507.tar.gz cs-y13-4531afb7137e85dbdc45ec9147612a101568a507.tar.bz2 cs-y13-4531afb7137e85dbdc45ec9147612a101568a507.zip |
chore: move and rename
Diffstat (limited to 'algorithms/binary_sort.py')
-rw-r--r-- | algorithms/binary_sort.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/algorithms/binary_sort.py b/algorithms/binary_sort.py new file mode 100644 index 0000000..2e71b52 --- /dev/null +++ b/algorithms/binary_sort.py @@ -0,0 +1,73 @@ +import random +def generate_test_data(length: int, _range: tuple): + arr = [] + for i in range(length): + arr.append(random.randint(int(_range[0]), int(_range[1]))) + return arr +def binsearch(arr, item): + arr = sorted(arr) + print(arr) + ubound, lbound = len(arr) - 1, 0 + found = False + while not found and lbound <= ubound: + index = (lbound + ubound) // 2 + if arr[index] == item: + found = True + print("Found here") + return found + if arr[index] > item: + ubound = index - 1 + if arr[index] < item: + lbound = index + 1 + print("not found") + return found + + +def linear_search_while(arr: list, item): + found: bool = False + index = 0 + while not found and index < len(arr): + if (arr[index] == item): + found = True + break + index += 1 + return (found, index) +def linear_search(arr: list, item): + index = 0 + for _item in arr: + if _item == item: + return (True, index) + index += 1 + return False +def test_existsness(samples: int): + yes = 0 + no = 0 + for i in range(samples): + query = random.randint(0, 100) + array = generate_test_data(100, (0, 100)) + found = linear_search(array, query) + if found: + yes += 1 + else: + no += 1 + print(f"In {samples} samples, {yes} match, {no} don't") +def main(): + narr = sorted(["A", "B", "C", "D", "E"]) + print("Hello world") + array = generate_test_data(10, (0, 100)) + print(array) + query = int(input("Your query for item: ")) + qstring = input("String item: ") + binsearch(narr, query) + found1 = linear_search(array, query) + found2 = linear_search_while(array, query) + if found1[0]: + print("Found by for loop method ", found1[1]) + else: print("Not found.") + if found2[0]: + print("Found by while loop method ", found2[1]) + else: + print("Not found.") + test_existsness(100) +if __name__ == "__main__": + main() |