Skip to content

Instantly share code, notes, and snippets.

@koehlma
Created October 26, 2014 09:41
Show Gist options
  • Save koehlma/575eae13bc4b3707e4e0 to your computer and use it in GitHub Desktop.
Save koehlma/575eae13bc4b3707e4e0 to your computer and use it in GitHub Desktop.
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