From 27d6a633cf7701518bed875f822c2561163f71b9 Mon Sep 17 00:00:00 2001 From: Oliver Hofmann Date: Fri, 11 Apr 2025 14:09:33 +0200 Subject: [PATCH] =?UTF-8?q?Counting=20Sort=20und=20bin=C3=A4re=20Suche?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/AlgoDatSoSe25.iml | 2 +- .../count_sorting.py | 60 ++++++++++++++++++ vorlesung/05_binaere_baeume/bin_search.py | 61 +++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 vorlesung/04_besondere_sortierverfahren/count_sorting.py create mode 100644 vorlesung/05_binaere_baeume/bin_search.py diff --git a/.idea/AlgoDatSoSe25.iml b/.idea/AlgoDatSoSe25.iml index 982282a..bf024fa 100644 --- a/.idea/AlgoDatSoSe25.iml +++ b/.idea/AlgoDatSoSe25.iml @@ -5,7 +5,7 @@ - + \ No newline at end of file diff --git a/vorlesung/04_besondere_sortierverfahren/count_sorting.py b/vorlesung/04_besondere_sortierverfahren/count_sorting.py new file mode 100644 index 0000000..507115d --- /dev/null +++ b/vorlesung/04_besondere_sortierverfahren/count_sorting.py @@ -0,0 +1,60 @@ +from utils.memory_array import MemoryArray +from utils.memory_cell import MemoryCell +from utils.memory_manager import MemoryManager +from utils.memory_range import mrange +from utils.literal import Literal + + + +def count_sort(a: MemoryArray, b: MemoryArray, k: int): + c = MemoryArray(Literal(k + 1)) + for i in mrange(Literal(k + 1)): + c[i].set(Literal(0)) + + for j in mrange(a.length()): + c[a[j]].set(c[a[j]].succ()) + + for i in mrange(Literal(1), Literal(k + 1)): + c[i].set(int(c[i]) + int(c[i.pred()])) + + for j in mrange(a.length().pred(), Literal(-1), Literal(-1)): + b[c[a[j]].pred()].set(a[j]) + c[a[j]].set(c[a[j]].pred()) + + + +def analyze_complexity(sizes, presorted=False): + """ + Analysiert die Komplexität einer Sortierfunktion. + + :param sizes: Eine Liste von Eingabegrößen für die Analyse. + """ + for size in sizes: + MemoryManager.purge() # Speicher zurücksetzen + if presorted: + random_array = MemoryArray.create_sorted_array(size, 0, 100) + else: + random_array = MemoryArray.create_random_array(size, 0, 100) + dest_array = MemoryArray(Literal(size)) + count_sort(random_array, dest_array, 100) + MemoryManager.save_stats(size) + + MemoryManager.plot_stats(["cells", "compares", "writes"]) + + +def swap(z: MemoryArray, i: int, j: int): + tmp = z[Literal(i)].value + z[Literal(i)] = z[Literal(j)] + z[Literal(j)].set(tmp) + + +if __name__ == '__main__': + + # Test the count_sort function + a = MemoryArray([2, 5, 3, 0, 2, 3, 0, 3]) + b = MemoryArray(Literal(len(a))) + count_sort(a, b, 5) + + sizes = range(10, 101, 10) + analyze_complexity(sizes) + # analyze_complexity(sizes, True) diff --git a/vorlesung/05_binaere_baeume/bin_search.py b/vorlesung/05_binaere_baeume/bin_search.py new file mode 100644 index 0000000..1bb0299 --- /dev/null +++ b/vorlesung/05_binaere_baeume/bin_search.py @@ -0,0 +1,61 @@ +import random + +from utils.memory_array import MemoryArray +from utils.memory_cell import MemoryCell +from utils.memory_manager import MemoryManager +from utils.memory_range import mrange +from utils.literal import Literal + + +def binary_search(z: MemoryArray, s: MemoryCell, l: Literal = None, r: Literal = None): + """ + Perform a binary search on the sorted array z for the value x. + """ + if l is None: + l = Literal(0) + if r is None: + r = Literal(z.length().pred()) + if l > r: + return None + with MemoryCell(l) as m: + m += r + m //= Literal(2) + if s < z[m]: + return binary_search(z, s, l, m.pred()) + elif s > z[m]: + return binary_search(z, s, m.succ(), r) + else: + return m + + +def analyze_complexity(sizes): + """ + Analysiert die Komplexität + + :param sizes: Eine Liste von Eingabegrößen für die Analyse. + """ + for size in sizes: + MemoryManager.purge() # Speicher zurücksetzen + random_array = MemoryArray.create_sorted_array(size) + search_value = random.randint(-100, 100) + binary_search(random_array, MemoryCell(search_value)) + MemoryManager.save_stats(size) + + MemoryManager.plot_stats(["cells", "compares", "adds"]) + + + + +if __name__ == "__main__": + # Example usage + arr = MemoryArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + search_value = MemoryCell(8) + result = binary_search(arr, search_value) + if result is not None: + print(f"Value {search_value} found at index {result}.") + else: + print(f"Value {search_value} not found in the array.") + + + sizes = range(1, 1001, 2) + analyze_complexity(sizes)