diff --git a/ShellSort.py b/ShellSort.py new file mode 100644 index 0000000..f38be0e --- /dev/null +++ b/ShellSort.py @@ -0,0 +1,36 @@ +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) \ No newline at end of file