Browse Source

Lecture 3

master
Oliver Hofmann 7 months ago
parent
commit
5ec825647c

+ 2
- 31
SoSe24/lec03_sort_alg/merge_plot.py View File

@@ -1,37 +1,8 @@
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, read_int_sequence_limited
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__":
filename = "../../seq3.txt"
@@ -46,7 +17,7 @@ if __name__ == "__main__":
AlgoDatValue.reset()
z = read_int_sequence_limited(filename, right_end)
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)
compare_values.append(AlgoDatValue.compare)


+ 27
- 0
SoSe24/lec03_sort_alg/quick_plot.py View File

@@ -0,0 +1,27 @@
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence, read_int_sequence_limited
import matplotlib.pyplot as plt
import quick_sort as qs




if __name__ == "__main__":
filename = "../../seq3.txt"
dummy = read_int_sequence("../../seq3.txt")
n = len(dummy)
step = n // 100

memory_values = []
compare_values = []

for right_end in range(1, n, step):
AlgoDatValue.reset()
z = read_int_sequence_limited(filename, right_end)
qs.quick_sort(z, 0, right_end-1)
memory_values.append(AlgoDatValue.memory)
compare_values.append(AlgoDatValue.compare)

plt.plot(range(1, n, step), memory_values, 'b', label='Memory')
plt.plot(range(1, n, step), compare_values, 'r', label='Compare')
plt.legend()
plt.show()

+ 39
- 0
SoSe24/lec03_sort_alg/quick_sort.py View File

@@ -0,0 +1,39 @@
from SoSe24.algodat.foundation import AlgoDatArray, AlgoDatValue, read_int_sequence
from time import perf_counter as pfc

def partition(z: AlgoDatArray, left: int, right: int) -> int:
global pivot
pivot.value = z[right]
i = left
j = right - 1
while i < j:
while z[i] < pivot:
i += 1
while z[j] > pivot:
j -= 1
if i < j:
z[i], z[j] = z[j], z[i]
i += 1
j -= 1
if i==j and z[i] < pivot:
i += 1
if z[i] != pivot:
z[i], z[right] = z[right], z[i]
return i

def quick_sort(z: AlgoDatArray, left: int, right: int):
if left < right:
q = partition(z, left, right)
quick_sort(z, left, q-1)
quick_sort(z, q+1, right)

pivot = AlgoDatValue(0)

if __name__ == "__main__":
z = read_int_sequence("../../seq0.txt")
print(z, len(z))
start = pfc()
quick_sort(z, 0, z.size-1)
print(z)
print(f"Dauer: {pfc() - start:.4f}s")
AlgoDatValue.summary()

Loading…
Cancel
Save