Created
June 9, 2018 09:57
-
-
Save whiledoing/4d86098f83bde843ccb521f8c63ecb9b to your computer and use it in GitHub Desktop.
[python-quick-sort] quick sort implmentation #python #algorithm
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 qsort(nums): | |
qsort_impl(nums, 0, len(nums)-1) | |
return nums | |
def qsort_impl(nums, start, end): | |
if start >= end: return | |
pivot = partition(nums, start, end) | |
qsort_impl(nums, start, pivot-1) | |
qsort_impl(nums, pivot+1, end) | |
def partition(nums, start, end): | |
if start >= end: return end | |
i_v, v = start, nums[start] | |
while start <= end: | |
while start <= end and nums[start] <= v: start += 1 | |
while start <= end and nums[end] > v: end -= 1 | |
if start > end: break | |
nums[start], nums[end] = nums[end], nums[start] | |
start += 1 | |
end -= 1 | |
nums[end], nums[i_v] = nums[i_v], nums[end] | |
return end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment