Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Created July 28, 2020 05:11
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 KerryJones/aa52514efbbad841e427cd591c0948a7 to your computer and use it in GitHub Desktop.
Save KerryJones/aa52514efbbad841e427cd591c0948a7 to your computer and use it in GitHub Desktop.
from typing import List
def quick_select(array: List, start: int, end: int, kth: int):
if start > end:
return
pivot = partition(array, start, end)
if pivot == kth - 1:
return array[pivot]
elif pivot > kth - 1:
return quick_select(array, start, pivot - 1, kth)
else:
return quick_select(array, pivot + 1, end, kth)
def partition(array: List, start: int, end: int) -> int:
pivot = start
for i in range(start, end):
if array[i] < array[end]:
array[i], array[pivot] = array[pivot], array[i]
pivot += 1
array[pivot], array[end] = array[end], array[pivot]
return pivot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment