Created
October 26, 2014 09:41
-
-
Save koehlma/575eae13bc4b3707e4e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def partition(liste, links, rechts, pivot): | |
b = links - 1 | |
counter = 0 | |
for k in range(links, rechts + 1): | |
liste[k], liste[b + 1] = liste[b + 1], liste[k] | |
counter += 1 | |
if liste[b + 1] <= pivot: | |
b += 1 | |
return b, counter | |
def quicksort_last(liste, links=0, rechts=None): | |
rechts = len(liste) - 1 if rechts is None else rechts | |
if links < rechts: | |
p, counter = partition(liste, links, rechts, liste[rechts]) | |
counter += quicksort_last(liste, links, p - 1) | |
counter += quicksort_last(liste, p + 1, rechts) | |
return counter | |
else: | |
return 0 | |
def quicksort_first(liste, links=0, rechts=None): | |
rechts = len(liste) - 1 if rechts is None else rechts | |
if links < rechts: | |
p, counter = partition(liste, links, rechts, liste[links]) | |
counter += quicksort_first(liste, links, p - 1) | |
counter += quicksort_first(liste, p + 1, rechts) | |
return counter | |
else: | |
return 0 | |
def insertionsort(liste): | |
counter = 0 | |
for i in range(1, len(liste)): | |
x, j = liste[i], i | |
while j > 0: | |
counter += 1 | |
if not liste[j - 1] > x: break | |
liste[j] = liste[j - 1] | |
j -= 1 | |
liste[j] = x | |
return counter | |
liste = [23, 11, 8, 4, 15] | |
print(quicksort_first(liste[:])) | |
print(quicksort_last(liste[:])) | |
print(insertionsort(liste[:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment