From e8feb3ca60cf2904ef7ea4103c6fa33ccd3f6de5 Mon Sep 17 00:00:00 2001 From: hofmannol Date: Tue, 9 Apr 2024 17:04:49 +0200 Subject: [PATCH] merge noch fehlerhaft --- SoSe24/lec03_sort_alg/merge_sort.py | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 SoSe24/lec03_sort_alg/merge_sort.py diff --git a/SoSe24/lec03_sort_alg/merge_sort.py b/SoSe24/lec03_sort_alg/merge_sort.py new file mode 100644 index 0000000..3d5cc94 --- /dev/null +++ b/SoSe24/lec03_sort_alg/merge_sort.py @@ -0,0 +1,54 @@ +from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf +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 + 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] + left_ptr += 1 + storage_ptr += 1 + else: + print(" Genommen: ", z[right_ptr]) + storage[storage_ptr] = z[right_ptr] + right_ptr += 1 + storage_ptr += 1 + elif left_ptr <= mid: + storage[storage_ptr] = z[left_ptr] + left_ptr += 1 + storage_ptr += 1 + else: + storage[storage_ptr] = z[right_ptr] + right_ptr += 1 + storage_ptr += 1 + for i in range(left, right+1): + z[i] = storage[i] + + +if __name__ == "__main__": + z = read_int_sequence("../../seq0.txt") + start = pfc() + storage = AlgoDatArray(z.size) + AlgoDatValue.summary() + merge_sort(z, storage, 0, z.size-1) + print(z) + print(f"Dauer: {pfc() - start:.4f}s") + AlgoDatValue.summary()