36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
import random
|
|
|
|
anzahlElemente = 10
|
|
zahlenListe = [random.randint(1, anzahlElemente - 1) for _ in range(anzahlElemente)]
|
|
output = False
|
|
|
|
def shell_sort(zahlenListe):
|
|
anzahlElemente = len(zahlenListe)
|
|
lücke = anzahlElemente // 2
|
|
|
|
while lücke > 0:
|
|
if (output):
|
|
print(f"\n--- Lückengröße: {lücke} ---")
|
|
|
|
for aktuellePosition in range(lücke, anzahlElemente):
|
|
einzufügendesElement = zahlenListe[aktuellePosition]
|
|
vergleichsPosition = aktuellePosition
|
|
|
|
while vergleichsPosition >= lücke and zahlenListe[vergleichsPosition - lücke] > einzufügendesElement:
|
|
if (output):
|
|
print(f" Vergleich: {zahlenListe[vergleichsPosition - lücke]} > {einzufügendesElement} → verschiebe nach rechts")
|
|
zahlenListe[vergleichsPosition] = zahlenListe[vergleichsPosition - lücke]
|
|
vergleichsPosition -= lücke
|
|
|
|
zahlenListe[vergleichsPosition] = einzufügendesElement
|
|
if (output):
|
|
print(f" Einsetzen: {einzufügendesElement} an Position {vergleichsPosition} → {zahlenListe}")
|
|
|
|
lücke //= 2
|
|
|
|
return zahlenListe
|
|
|
|
|
|
print("Unsortiert:", zahlenListe)
|
|
sortierteListe = shell_sort(zahlenListe)
|
|
print("\nSortiert: ", sortierteListe) |