Counting Sort und binäre Suche

This commit is contained in:
Oliver Hofmann 2025-04-11 14:09:33 +02:00
parent 4aeb01c003
commit 27d6a633cf
3 changed files with 122 additions and 1 deletions

View File

@ -5,7 +5,7 @@
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.12 (SoSe25)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.12 (AlgoDatSoSe25)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

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

View File

@ -0,0 +1,61 @@
import random
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 binary_search(z: MemoryArray, s: MemoryCell, l: Literal = None, r: Literal = None):
"""
Perform a binary search on the sorted array z for the value x.
"""
if l is None:
l = Literal(0)
if r is None:
r = Literal(z.length().pred())
if l > r:
return None
with MemoryCell(l) as m:
m += r
m //= Literal(2)
if s < z[m]:
return binary_search(z, s, l, m.pred())
elif s > z[m]:
return binary_search(z, s, m.succ(), r)
else:
return m
def analyze_complexity(sizes):
"""
Analysiert die Komplexität
:param sizes: Eine Liste von Eingabegrößen für die Analyse.
"""
for size in sizes:
MemoryManager.purge() # Speicher zurücksetzen
random_array = MemoryArray.create_sorted_array(size)
search_value = random.randint(-100, 100)
binary_search(random_array, MemoryCell(search_value))
MemoryManager.save_stats(size)
MemoryManager.plot_stats(["cells", "compares", "adds"])
if __name__ == "__main__":
# Example usage
arr = MemoryArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
search_value = MemoryCell(8)
result = binary_search(arr, search_value)
if result is not None:
print(f"Value {search_value} found at index {result}.")
else:
print(f"Value {search_value} not found in the array.")
sizes = range(1, 1001, 2)
analyze_complexity(sizes)