From 06bf0a5bf4ecf89793bae98b9d868222df52b451 Mon Sep 17 00:00:00 2001 From: percy07 <56677891+percy07@users.noreply.github.com> Date: Wed, 30 Oct 2019 14:53:59 +0530 Subject: [PATCH 1/3] Update quick_select.py Add Doctests. --- searches/quick_select.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/searches/quick_select.py b/searches/quick_select.py index 6b70562bd78f..08a3e8969c41 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -26,6 +26,16 @@ def _partition(data, pivot): def quickSelect(list, k): + """ + >>> quickSelect([2,4,5,7,899,54,32], 5) + 54 + >>> quickSelect([2,4,5,7,899,54,32], 1) + 4 + >>> quickSelect([5,4,3,2], 2) + 4 + >>> quickSelect([3,5,7,10,2,12],3) + 7 + """ # k = len(list) // 2 when trying to find the median (index that value would be when list is sorted) # invalid input From f8f78cb8998a09943ee9e1c100edd80970d6529c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 30 Oct 2019 15:51:27 +0100 Subject: [PATCH 2/3] Add typehints --- searches/quick_select.py | 42 +++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/searches/quick_select.py b/searches/quick_select.py index 08a3e8969c41..f6e44e6df8f4 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -1,12 +1,13 @@ -import random - """ -A python implementation of the quick select algorithm, which is efficient for calculating the value that would appear in the index of a list if it would be sorted, even if it is not already sorted +A Python implementation of the quick select algorithm, which is efficient for +calculating the value that would appear in the index of a list if it would be +sorted, even if it is not already sorted https://en.wikipedia.org/wiki/Quickselect """ +import random -def _partition(data, pivot): +def _partition(data: list, pivot) -> tuple: """ Three way partition the data into smaller, equal and greater lists, in relationship to the pivot @@ -25,38 +26,39 @@ def _partition(data, pivot): return less, equal, greater -def quickSelect(list, k): +def quick_select(items: list, index: int): """ - >>> quickSelect([2,4,5,7,899,54,32], 5) + >>> quick_select([2, 4, 5, 7, 899, 54, 32], 5) 54 - >>> quickSelect([2,4,5,7,899,54,32], 1) + >>> quick_select([2, 4, 5, 7, 899, 54, 32], 1) 4 - >>> quickSelect([5,4,3,2], 2) + >>> quick_select([5, 4, 3, 2], 2) 4 - >>> quickSelect([3,5,7,10,2,12],3) + >>> quick_select([3, 5, 7, 10, 2, 12], 3) 7 """ - # k = len(list) // 2 when trying to find the median (index that value would be when list is sorted) + # index = len(items) // 2 when trying to find the median + # (value of index when items is sorted) # invalid input - if k >= len(list) or k < 0: + if index >= len(items) or index < 0: return None - smaller = [] - larger = [] - pivot = random.randint(0, len(list) - 1) - pivot = list[pivot] + smaller: list = [] + larger: list = [] + pivot = random.randint(0, len(items) - 1) + pivot = items[pivot] count = 0 - smaller, equal, larger = _partition(list, pivot) + smaller, equal, larger = _partition(items, pivot) count = len(equal) m = len(smaller) # k is the pivot - if m <= k < m + count: + if m <= index < m + count: return pivot # must be in smaller - elif m > k: - return quickSelect(smaller, k) + elif m > index: + return quick_select(smaller, index) # must be in larger else: - return quickSelect(larger, k - (m + count)) + return quick_select(larger, index - (m + count)) From ed3373660e5b468280670db1283effb26bf1d300 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 30 Oct 2019 16:06:12 +0100 Subject: [PATCH 3/3] Don't pre-allocate "smaller" and "larger" --- searches/quick_select.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/searches/quick_select.py b/searches/quick_select.py index f6e44e6df8f4..17dca395f73c 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -44,8 +44,6 @@ def quick_select(items: list, index: int): if index >= len(items) or index < 0: return None - smaller: list = [] - larger: list = [] pivot = random.randint(0, len(items) - 1) pivot = items[pivot] count = 0 @@ -53,7 +51,7 @@ def quick_select(items: list, index: int): count = len(equal) m = len(smaller) - # k is the pivot + # index is the pivot if m <= index < m + count: return pivot # must be in smaller