fork download
  1. def quicksort(array):
  2. # Базовый случай: список с 0 или 1 элементом уже «отсортирован»
  3. if len(array) < 2:
  4. return array
  5.  
  6. # Рекурсивный случай
  7. pivot = array[0]
  8. less = [i for i in array[1:] if i <= pivot] # Элементы меньше или равные опорному
  9. greater = [i for i in array[1:] if i > pivot] # Элементы больше опорного
  10.  
  11. return quicksort(less) + [pivot] + quicksort(greater)
  12.  
  13. # Тест
  14. print(quicksort([10, 5, 6, 5, 2, 3]))
  15.  
Success #stdin #stdout 0.13s 14172KB
stdin
Standard input is empty
stdout
[2, 3, 5, 5, 6, 10]