This commit is contained in:
alstergym
2026-09-28 15:50:13 +02:00
parent 075452b8e7
commit 6a176d7f00
4 changed files with 149 additions and 9 deletions
+46
View File
@@ -0,0 +1,46 @@
import random
import time
def bubblesort(array):
anzahlVergleiche = 0
anzahlTauschvorgaenge = 0
n = len(array)
startzeit = time.perf_counter()
for i in range(n - 1):
for j in range(n - 1):
anzahlVergleiche += 1
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
anzahlTauschvorgaenge += 1
endzeit = time.perf_counter()
return anzahlVergleiche, anzahlTauschvorgaenge, endzeit - startzeit
# 40.000 unterschiedliche Zahlen zwischen 1 und 100.000
data = random.sample(range(1, 100001), 40000)
print("=" * 45)
print(" BUBBLESORT")
print("=" * 45)
vergleiche, tausche, zeit = bubblesort(data)
print("\nSortierung abgeschlossen!")
print("-" * 45)
print("Anzahl Zahlen: ", len(data))
print("Anzahl Vergleiche: ", vergleiche)
print("Anzahl Tauschvorgänge:", tausche)
print("Benötigte Zeit: ", zeit, "Sekunden")
print("-" * 45)
print("\nErste 20 Zahlen des sortierten Arrays:")
print(data[:20])
print("=" * 45)
+44
View File
@@ -0,0 +1,44 @@
import random
import time
def quicksort(array, links, rechts):
if links < rechts:
pivot = array[rechts]
i = links - 1
for j in range(links, rechts):
if array[j] <= pivot:
i += 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[rechts] = array[rechts], array[i + 1]
quicksort(array, links, i)
quicksort(array, i + 2, rechts)
# 1000 unterschiedliche Zahlen zwischen 1 und 100.000
data = random.sample(range(1, 100001), 1000)
print("=" * 40)
print(" QUICKSORT")
print("=" * 40)
startzeit = time.perf_counter()
quicksort(data, 0, len(data) - 1)
endzeit = time.perf_counter()
print("\nSortierung abgeschlossen!")
print("-" * 40)
print("Anzahl Zahlen: ", len(data))
print("Benötigte Zeit: ", endzeit - startzeit, "Sekunden")
print("-" * 40)
print("\nErste 20 Zahlen des sortierten Arrays:")
print(data[:20])
print("=" * 40)
+51
View File
@@ -0,0 +1,51 @@
import random
import time
def selectionsort(array):
anzahlVergleiche = 0
anzahlTauschvorgaenge = 0
n = len(array)
startzeit = time.perf_counter()
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
anzahlVergleiche += 1
if array[j] < array[min_index]:
min_index = j
if min_index != i:
array[i], array[min_index] = array[min_index], array[i]
anzahlTauschvorgaenge += 1
endzeit = time.perf_counter()
return anzahlVergleiche, anzahlTauschvorgaenge, endzeit - startzeit
# 1000 unterschiedliche Zahlen zwischen 1 und 100.000
data = random.sample(range(1, 100001), 100000)
print("=" * 45)
print(" SELECTIONSORT")
print("=" * 45)
vergleiche, tausche, zeit = selectionsort(data)
print("\nSortierung abgeschlossen!")
print("-" * 45)
print("Anzahl Zahlen: ", len(data))
print("Anzahl Vergleiche: ", vergleiche)
print("Anzahl Tauschvorgänge:", tausche)
print("Benötigte Zeit: ", zeit, "Sekunden")
print("-" * 45)
print("\nErste 20 Zahlen des sortierten Arrays:")
print(data[:20])
print("=" * 45)
+8 -9
View File
@@ -1,14 +1,14 @@
import turtle import turtle
#def zeichnen(n): def zeichnen(n):
# if n > 0: if n > 0:
# turtle.forward(300) turtle.forward(300)
# turtle.left(75) turtle.left(75)
# turtle.forward(150) turtle.forward(150)
# turtle.backward(150) turtle.backward(150)
# zeichnen(n - 1) zeichnen(n - 1)
#zeichnen(2) zeichnen(2)
#turtle.mainloop() #turtle.mainloop()
@@ -32,7 +32,6 @@ import turtle
#turtle.mainloop() #turtle.mainloop()
def baum(laenge): def baum(laenge):
if laenge < 20: if laenge < 20:
return return