Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # QuickSort
- # Paramètre
- L = [5, 4, 3, 2, 1, 0, 6, 7, 8, 9]
- # Résultat
- # list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- def quicksort(L):
- if len(L) == 0:
- # []
- return L
- if len(L) == 1:
- # [a]
- return L
- pivot = L[0]
- minor = []
- major = []
- for i in range(1, len(L)):
- if L[i] < pivot:
- minor.append(L[i])
- if L[i] == pivot:
- minor.append(L[i])
- if L[i] > pivot:
- major.append(L[i])
- return quicksort(minor) + [pivot] + quicksort(major)
- def quicksort_compact(L):
- if len(L) < 1:
- return L
- pivot = L[0]
- minor = [x for x in L[1:] if x <= pivot]
- major = [x for x in L[1:] if x > pivot]
- return quicksort(minor) + [pivot] + quicksort(major)
- print(L)
- sortedL = quicksort_compact(L)
- print(sortedL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement