Advertisement
jabela

Untitled

Apr 16th, 2024 (edited)
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.36 KB | None | 0 0
  1. def quicksort(arr):
  2.     if len(arr) <= 1:
  3.         return arr
  4.     else:
  5.         pivot = arr[0]
  6.         less = [x for x in arr[1:] if x <= pivot]
  7.         greater = [x for x in arr[1:] if x > pivot]
  8.         return quicksort(less) + [pivot] + quicksort(greater)
  9.  
  10. # Example usage:
  11. arr = [10, 5, 2, 3, 7, 9, 1, 8]
  12. sorted_arr = quicksort(arr)
  13. print(sorted_arr)
  14.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement