Created
January 25, 2018 21:40
-
-
Save conr/b6b25bb93dd10912a5700e72324634b4 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
# Quick Sort - Runtime: Average O(n log(n)), Worst O(n^2), Memory O(log(n)) | |
def quick_sort(arr, left, right) | |
index = partition(arr, left, right) | |
if left > index - 1 | |
quick_sort(arr, left, index - 1) | |
end | |
if index < right | |
quick_sort(arr, index, right) | |
end | |
end | |
def partition(arr, left, right) | |
pivot = arr[(left+right)/2] | |
while left <= right | |
while arr[left] < arr[index] | |
left += 1 | |
end | |
while arr[right] > arr[index] | |
right -=1 | |
end | |
if left <= right | |
arr[left], arr[right] = arr[right], arr[left] | |
left += 1 | |
right -=1 | |
end | |
end | |
return left | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment