Dies ist eine alte Version des Dokuments!


Sortieralgorithmen in Python

# webtigerjython

from gpanel import *
import random 
import time

makeGPanel(0,600,0,400)

# Liste der Zahlen von 0 bis 99 in zufälliger Reihenfolge
zufallsListe = random.sample(range(0,100),100)

setColor("red")

#===============================================================
# Hilfsfunktion drawList, zeichnet ein Diagramm einer Liste
#===============================================================

def drawList(l,sleeptime):
    clear()
    x = 20
    lineWidth(5) 
    for i in l:
        line(x,30,x,i+30)
        x = x + 5
    time.sleep(sleeptime)

def bubbleSort(list1):
    for i in range(len(list1)-1,0,-1): #i läuft rückwärts
        for j in range(0,i,1):
            drawList(list1,0.01)
            if list1[j]>list1[j+1]:
                list1[j],list1[j+1] = list1[j+1],list1[j]

bubbleSort(zufallsListe)

#===============================================================
# Selection-Sort (Children-Sort)
#===============================================================

def getSmallestIndex(Liste1):
    smallestElement = Liste1[0]
    smallestIndex = 0
    for i in range(len(Liste1)):
        if Liste1[i] < smallestElement:
            smallestElement = Liste1[i]
            smallestIndex = i
    return smallestIndex
    
def selectionSort(Liste1):
    for i in range(len(Liste1)): 
        drawList(Liste1,0.1)
        index = getSmallestIndex(Liste1[i:]) + i 
        Liste1[i],Liste1[index] = Liste1[index],Liste1[i]

  • ef/algorithmen/sortieralgorithmen.1713445431.txt.gz
  • Zuletzt geändert: 2024/04/18 15:03
  • von lehmannr