shellsort
This commit is contained in:
1 parent
8d5cb700f5
commit
3a90e78f94
1 file changed
+36
@@ -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)
|
||||
Reference in new issue
Block a user