Created
July 28, 2020 05:11
-
-
Save KerryJones/aa52514efbbad841e427cd591c0948a7 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
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