Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. # QuickSort
  2.  
  3. # Paramètre
  4.  
  5. L = [5, 4, 3, 2, 1, 0, 6, 7, 8, 9]
  6.  
  7.  
  8. # Résultat
  9.  
  10. # list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  11.  
  12. def quicksort(L):
  13.     if len(L) == 0:
  14.         # []
  15.         return L
  16.     if len(L) == 1:
  17.         # [a]
  18.         return L
  19.     pivot = L[0]
  20.     minor = []
  21.     major = []
  22.     for i in range(1, len(L)):
  23.         if L[i] < pivot:
  24.             minor.append(L[i])
  25.         if L[i] == pivot:
  26.             minor.append(L[i])
  27.         if L[i] > pivot:
  28.             major.append(L[i])
  29.     return quicksort(minor) + [pivot] + quicksort(major)
  30.  
  31.  
  32. def quicksort_compact(L):
  33.     if len(L) < 1:
  34.         return L
  35.     pivot = L[0]
  36.     minor = [x for x in L[1:] if x <= pivot]
  37.     major = [x for x in L[1:] if x > pivot]
  38.     return quicksort(minor) + [pivot] + quicksort(major)
  39.  
  40.  
  41. print(L)
  42. sortedL = quicksort_compact(L)
  43. print(sortedL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement