PDA

View Full Version : [Frage] qsort und mergesort in python


mkdigital
06-11-2007, 14:38
hi!

habe das mal schnell in python gecodet. wer will, der nehme!

lg

tonico
06-11-2007, 15:19
hi!

habe das mal schnell in python gecodet. wer will, der nehme!

lg

Hey cool! Hier noch ein Vorschlag für Quicksort mit einer alternativen Implementierung von "partition" (Pseudocode-Vorlage aus dem Buch Introduction to algorithms.)


def quicksort(A, l, r):
if l < r:
p = partition(A, l, r)
quicksort(A, l, (p - 1))
quicksort(A, (p + 1), r)

def partition(A, l, r):
x = A[r]
i = l - 1
for j in range(l, r):
if A[j] <= x:
i = i + 1
exchange(A, i, j)

i = i + 1
exchange(A, i, r)
return i

def exchange(A, i, j):
val = A[i]
A[i] = A[j]
A[j] = val

input = [44, 55, 12, 42, 94, 18, 6, 67]
quicksort(input, 0, (len(input) - 1))
print input

Ich finde Python ist zum Lernen viel angenehmer.