Created
December 13, 2019 04:59
-
-
Save SuvroBaner/9dc81bd390464800569d1da5ef8b89fa to your computer and use it in GitHub Desktop.
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
def partition(arr, low, high): | |
pivot = arr[high] # pivot is the last element of the array | |
i = (low - 1) # index of the smaller element, it starts with -1 | |
# j : increments through the array and checks with the pivot if it is greater or smaller | |
for j in range(low, high): | |
if arr[j] <= pivot: | |
i = i + 1 | |
# swapping logic | |
arr[i], arr[j] = arr[j], arr[i] | |
arr[i+1], arr[high] = arr[high], arr[i+1] # swaping the pivot value now | |
return (i+1) | |
def quicksort(arr, low, high): | |
if low < high: | |
p = partition(arr, low, high) | |
quicksort(arr, low, p - 1) | |
quicksort(arr, p + 1, high) | |
arr = [9, 3, 1, 0, 3, 10, 0, 22, 16, 15] | |
quicksort(arr, 0, len(arr)-1) | |
print('Sorted Array: ', arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment