Created
July 31, 2022 17:25
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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