Sortiere(Python)

Aus IT074-Wiki

Wechseln zu: Navigation, Suche

Iteration

def sortiere(data):
    for i in data:
        for j in range(0,len(data)-1):
        if data[j] <= data[j+1]:
        h = data[j]
        data[j] = data[j+1]
        data[j+1] = h
    return data
 
data = list()
data = [9,3,4,10,4]
 
print sortiere(data)

Rekursion

def sort(li):
	res = list()
	if len(li) > 0:
		res.append(min(li))
		li.remove(min(li))
		res.extend(sort(li))
	return res

Auf ähnliche Weise kann man in Haskell sortieren.

Shorties

sorted(Liste) 
# gibt eine sortierte Liste zurück (Out-of-place)
# Bsp: 
sorted([99,1,1,99,1]) #-> [1,1,1,99,99]
 
Liste.sort()
# sortiert (verändert) eine gegebene Liste (In-place)
# Bsp.: 
liste = [1, 5, 2, 7, 96]
liste.sort() #-> liste ist nun [1, 2, 5, 7, 96]