This commit is contained in:
Oliver Hofmann
2026-03-27 17:19:51 +01:00
commit 4f5a78ac05
55 changed files with 14110 additions and 0 deletions
@@ -0,0 +1,60 @@
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
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))
for j in mrange(a.length()):
c[a[j]].set(c[a[j]].succ())
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())
def analyze_complexity(sizes, presorted=False):
"""
Analysiert die Komplexität einer Sortierfunktion.
:param sizes: Eine Liste von Eingabegrößen für die Analyse.
"""
for size in sizes:
MemoryManager.purge() # Speicher zurücksetzen
if presorted:
random_array = MemoryArray.create_sorted_array(size, 0, 100)
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)
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)
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)
sizes = range(10, 101, 10)
analyze_complexity(sizes)
# analyze_complexity(sizes, True)