From 93c4fb91e546157de4a33d4da10156b6c8715e93 Mon Sep 17 00:00:00 2001 From: Oliver Hofmann Date: Thu, 11 Apr 2024 12:00:28 +0200 Subject: [PATCH] Lecture 2 --- SoSe24/lec03_sort_alg/merge_sort.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/SoSe24/lec03_sort_alg/merge_sort.py b/SoSe24/lec03_sort_alg/merge_sort.py index e8ca997..52b92d7 100644 --- a/SoSe24/lec03_sort_alg/merge_sort.py +++ b/SoSe24/lec03_sort_alg/merge_sort.py @@ -1,43 +1,43 @@ -from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, MinusInf +from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence from time import perf_counter as pfc -def merge_sort(z: AlgoDatArray, storage: AlgoDatArray, left: int, right: int): +def merge_sort(z: AlgoDatArray, buffer: AlgoDatArray, left: int, right: int): if left == right: return mid = (left + right) // 2 - merge_sort(z, storage, left, mid) - merge_sort(z, storage, mid+1, right) + merge_sort(z, buffer, left, mid) + merge_sort(z, buffer, mid + 1, right) left_ptr = left right_ptr = mid+1 storage_ptr = left while left_ptr <= mid or right_ptr <= right: if left_ptr <= mid and right_ptr <= right: if z[left_ptr] < z[right_ptr]: - storage[storage_ptr].value = z[left_ptr].value + buffer[storage_ptr].value = z[left_ptr].value left_ptr += 1 storage_ptr += 1 else: - storage[storage_ptr].value = z[right_ptr].value + buffer[storage_ptr].value = z[right_ptr].value right_ptr += 1 storage_ptr += 1 elif left_ptr <= mid: - storage[storage_ptr].value = z[left_ptr].value + buffer[storage_ptr].value = z[left_ptr].value left_ptr += 1 storage_ptr += 1 else: - storage[storage_ptr].value = z[right_ptr].value + buffer[storage_ptr].value = z[right_ptr].value right_ptr += 1 storage_ptr += 1 for i in range(left, right): - z[i].value = storage[i].value + z[i].value = buffer[i].value if __name__ == "__main__": z = read_int_sequence("../../seq0.txt") print(z, len(z)) start = pfc() - storage = AlgoDatArray(z.size) # Zwischenspeicher + storage = AlgoDatArray(z.size) # buffer AlgoDatValue.summary() merge_sort(z, storage, 0, z.size-1) print(z)