Skip to content

Instantly share code, notes, and snippets.

@whiledoing
Created June 9, 2018 09:57
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 whiledoing/4d86098f83bde843ccb521f8c63ecb9b to your computer and use it in GitHub Desktop.
Save whiledoing/4d86098f83bde843ccb521f8c63ecb9b to your computer and use it in GitHub Desktop.
[python-quick-sort] quick sort implmentation #python #algorithm
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