Browse Source

merge noch fehlerhaft

master
hofmannol 6 months ago
parent
commit
e8feb3ca60
1 changed files with 54 additions and 0 deletions
  1. 54
    0
      SoSe24/lec03_sort_alg/merge_sort.py

+ 54
- 0
SoSe24/lec03_sort_alg/merge_sort.py View File

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()

Loading…
Cancel
Save