Skip to content

Instantly share code, notes, and snippets.

@ioanzicu
Created July 31, 2022 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ioanzicu/4b7a2cf85182fb3c7a3e4f821da7a935 to your computer and use it in GitHub Desktop.
Save ioanzicu/4b7a2cf85182fb3c7a3e4f821da7a935 to your computer and use it in GitHub Desktop.
Quick sort algorithm implementation in Python 3. Average/Best complexity - O(n log n), worst case - O(n**2).
nums = [2, 3, 5, 6, 1, 4, 8, 7]
def quick_sort(array): # O(n log n)
if len(array) < 2:
return array
pivot = array[0]
left_arr = [i for i in array[1:] if i <= pivot]
right_arr = [i for i in array[1:] if i > pivot]
return quick_sort(left_arr) + [pivot] + quick_sort(right_arr)
res = quick_sort(nums)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment