Umstellen der Auswertungslogik

This commit is contained in:
Oliver Hofmann
2026-03-30 13:09:16 +02:00
parent 228273f399
commit c48b5c7e59
42 changed files with 1690 additions and 1564 deletions
@@ -1,60 +1,53 @@
from utils.memory_array import MemoryArray
from utils.memory_cell import MemoryCell
from utils.memory_manager import MemoryManager
from utils.memory_range import mrange
from utils.literal import Literal
from utils.algo_context import AlgoContext
from utils.algo_array import Array
from utils.algo_range import irange
def count_sort(a: Array, b: Array, k: int, ctx: AlgoContext):
"""
Counting Sort.
def count_sort(a: MemoryArray, b: MemoryArray, k: int):
c = MemoryArray(Literal(k + 1))
for i in mrange(Literal(k + 1)):
c[i].set(Literal(0))
a – Eingabe-Array mit Werten aus [0, k]
b – Ausgabe-Array (gleiche Länge wie a)
k – maximaler Wert in a
"""
c = Array([0] * (k + 1), ctx) # Zählarray
for j in mrange(a.length()):
c[a[j]].set(c[a[j]].succ())
# Häufigkeiten zählen
for j in irange(len(a)):
c[a[j]] = c[a[j]] + 1
for i in mrange(Literal(1), Literal(k + 1)):
c[i].set(int(c[i]) + int(c[i.pred()]))
for j in mrange(a.length().pred(), Literal(-1), Literal(-1)):
b[c[a[j]].pred()].set(a[j])
c[a[j]].set(c[a[j]].pred())
# Kumulierte Summen bilden
for i in irange(1, k + 1):
c[i] = c[i] + c[i - 1]
# Stabil in b einsortieren (rückwärts für Stabilität)
for j in irange(len(a) - 1, -1, -1):
b[c[a[j]] - 1] = a[j]
c[a[j]] = c[a[j]] - 1
def analyze_complexity(sizes, presorted=False):
"""
Analysiert die Komplexität einer Sortierfunktion.
:param sizes: Eine Liste von Eingabegrößen für die Analyse.
"""
ctx = AlgoContext()
for size in sizes:
MemoryManager.purge() # Speicher zurücksetzen
ctx.reset()
if presorted:
random_array = MemoryArray.create_sorted_array(size, 0, 100)
z = Array.sorted(size, ctx)
else:
random_array = MemoryArray.create_random_array(size, 0, 100)
dest_array = MemoryArray(Literal(size))
count_sort(random_array, dest_array, 100)
MemoryManager.save_stats(size)
z = Array.random(size, 0, 100, ctx)
dest = Array([0] * size, ctx)
count_sort(z, dest, 100, ctx)
ctx.save_stats(size)
MemoryManager.plot_stats(["cells", "compares", "writes"])
def swap(z: MemoryArray, i: int, j: int):
tmp = z[Literal(i)].value
z[Literal(i)] = z[Literal(j)]
z[Literal(j)].set(tmp)
ctx.plot_stats(["reads", "writes"])
if __name__ == '__main__':
# Test the count_sort function
a = MemoryArray([2, 5, 3, 0, 2, 3, 0, 3])
b = MemoryArray(Literal(len(a)))
count_sort(a, b, 5)
ctx = AlgoContext()
a = Array([2, 5, 3, 0, 2, 3, 0, 3], ctx)
b = Array([0] * len(a), ctx)
count_sort(a, b, 5, ctx)
print(b)
sizes = range(10, 101, 10)
analyze_complexity(sizes)
# analyze_complexity(sizes, True)