|
|
|
|
|
|
|
|
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, read_int_sequence_limited |
|
|
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, read_int_sequence_limited |
|
|
import matplotlib.pyplot as plt |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
import merge_sort as ms |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_sort(z: AlgoDatArray, storage: 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) |
|
|
|
|
|
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 |
|
|
|
|
|
left_ptr += 1 |
|
|
|
|
|
storage_ptr += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
storage[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 |
|
|
|
|
|
left_ptr += 1 |
|
|
|
|
|
storage_ptr += 1 |
|
|
|
|
|
else: |
|
|
|
|
|
storage[storage_ptr].value = z[right_ptr].value |
|
|
|
|
|
right_ptr += 1 |
|
|
|
|
|
storage_ptr += 1 |
|
|
|
|
|
for i in range(left, right+1): |
|
|
|
|
|
z[i].value = storage[i].value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|
filename = "../../seq3.txt" |
|
|
filename = "../../seq3.txt" |
|
|
|
|
|
|
|
|
AlgoDatValue.reset() |
|
|
AlgoDatValue.reset() |
|
|
z = read_int_sequence_limited(filename, right_end) |
|
|
z = read_int_sequence_limited(filename, right_end) |
|
|
storage = AlgoDatArray(z.size) |
|
|
storage = AlgoDatArray(z.size) |
|
|
merge_sort(z, storage, 0, right_end-1) |
|
|
|
|
|
|
|
|
ms.merge_sort(z, storage, 0, right_end-1) |
|
|
memory_values.append(AlgoDatValue.memory) |
|
|
memory_values.append(AlgoDatValue.memory) |
|
|
compare_values.append(AlgoDatValue.compare) |
|
|
compare_values.append(AlgoDatValue.compare) |
|
|
|
|
|
|