From 74f4161fe7397f75f9c63f61166513271199a3d3 Mon Sep 17 00:00:00 2001 From: Oliver Hofmann Date: Thu, 11 Apr 2024 10:05:53 +0200 Subject: [PATCH] Lecture 2 --- SoSe24/algodat/foundation.py | 3 +-- SoSe24/lec02_maxfolge/cubic.py | 2 +- SoSe24/lec03_sort_alg/merge_sort.py | 25 ++++++++----------------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/SoSe24/algodat/foundation.py b/SoSe24/algodat/foundation.py index b2a803a..15ba3fe 100644 --- a/SoSe24/algodat/foundation.py +++ b/SoSe24/algodat/foundation.py @@ -325,7 +325,6 @@ def read_int_sequence(filename: str) -> AlgoDatArray: with open(filename, "r") as file: l = list(map(int, file.read().split())) a = AlgoDatArray(len(l)) - AlgoDatValue.memory -= len(l) for i in range(len(l)): - a.set(i, AlgoDatValue(l[i])) + a[i].value = l[i] return a diff --git a/SoSe24/lec02_maxfolge/cubic.py b/SoSe24/lec02_maxfolge/cubic.py index 43182cf..8ff1dbc 100644 --- a/SoSe24/lec02_maxfolge/cubic.py +++ b/SoSe24/lec02_maxfolge/cubic.py @@ -23,4 +23,4 @@ if __name__ == "__main__": start = pfc() print(maxfolge1(z)) print(f"Dauer: {pfc() - start:.4f}s") - AlgoDatValue.summary() + AlgoDatValue.summary() \ No newline at end of file diff --git a/SoSe24/lec03_sort_alg/merge_sort.py b/SoSe24/lec03_sort_alg/merge_sort.py index 3d5cc94..7ea3c4a 100644 --- a/SoSe24/lec03_sort_alg/merge_sort.py +++ b/SoSe24/lec03_sort_alg/merge_sort.py @@ -3,50 +3,41 @@ from time import perf_counter as pfc def merge_sort(z: AlgoDatArray, storage: AlgoDatArray, left: int, right: int): - print(left, right) if left == right: return mid = (left + right) // 2 merge_sort(z, storage, left, mid) merge_sort(z, storage, mid+1, right) left_ptr = left - right_ptr = right + right_ptr = mid+1 storage_ptr = left - print("left: ", end="") - for i in range(left, mid+1): - print(z[i], " ", end="") - print(" right: ", end="") - for i in range(mid+1, right+1): - print(z[i], " ", end="") while left_ptr <= mid or right_ptr <= right: if left_ptr <= mid and right_ptr <= right: - print("Beides ", z[left_ptr], " ", z[right_ptr]) if z[left_ptr] < z[right_ptr]: - print(" Genommen: ", z[left_ptr]) - storage[storage_ptr] = z[left_ptr] + storage[storage_ptr].value = z[left_ptr].value left_ptr += 1 storage_ptr += 1 else: - print(" Genommen: ", z[right_ptr]) - storage[storage_ptr] = z[right_ptr] + storage[storage_ptr].value = z[right_ptr].value right_ptr += 1 storage_ptr += 1 elif left_ptr <= mid: - storage[storage_ptr] = z[left_ptr] + storage[storage_ptr].value = z[left_ptr].value left_ptr += 1 storage_ptr += 1 else: - storage[storage_ptr] = z[right_ptr] + storage[storage_ptr].value = z[right_ptr].value right_ptr += 1 storage_ptr += 1 for i in range(left, right+1): - z[i] = storage[i] + z[i].value = storage[i].value if __name__ == "__main__": z = read_int_sequence("../../seq0.txt") + print(z, len(z)) start = pfc() - storage = AlgoDatArray(z.size) + storage = AlgoDatArray(z.size) # Zwischenspeicher AlgoDatValue.summary() merge_sort(z, storage, 0, z.size-1) print(z)