From a30a2831e66cdd1d4e87d161b26c19512cadb738 Mon Sep 17 00:00:00 2001 From: Lewis Tian Date: Tue, 29 Sep 2020 11:32:10 +0800 Subject: [PATCH 1/4] Update sorts/quick_sort_3partition.py Another quick sort algorithm, returns a new sorted list --- sorts/quick_sort_3_partition.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/sorts/quick_sort_3_partition.py b/sorts/quick_sort_3_partition.py index a25ac7def802..dee3f25359dc 100644 --- a/sorts/quick_sort_3_partition.py +++ b/sorts/quick_sort_3_partition.py @@ -1,4 +1,4 @@ -def quick_sort_3partition(sorting, left, right): +def quick_sort_3partition(sorting: list, left: int, right: int) -> list: if right <= left: return a = i = left @@ -18,7 +18,33 @@ def quick_sort_3partition(sorting, left, right): quick_sort_3partition(sorting, b + 1, right) +def quick_sort_3partition(sorting: list) -> list: + """ + Another quick sort algorithm, returns a new sorted list + + >>> quick_sort_3partition([]) + [] + >>> quick_sort_3partition([1]) + [1] + >>> quick_sort_3partition([-5, -2, 1, -2, 0, 1]) + [-5, -2, -2, 0, 1, 1] + >>> quick_sort_3partition([1, 2, 5, 1, 2, 0, 0, 5, 2, -1]) + [-1, 0, 0, 1, 1, 2, 2, 2, 5, 5] + """ + if len(sorting) <= 1: + return sorting + return ( + quick_sort_3partition([i for i in sorting if i < sorting[0]]) + + [i for i in sorting if i == sorting[0]] + + quick_sort_3partition([i for i in sorting if i > sorting[0]]) + ) + + if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) + user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] quick_sort_3partition(unsorted, 0, len(unsorted) - 1) From 85f2e8166b09a189a439ba424a7a909dbadb2b8f Mon Sep 17 00:00:00 2001 From: Lewis Tian Date: Tue, 29 Sep 2020 14:39:18 +0800 Subject: [PATCH 2/4] Update sorts/quick_sort_3_partition.py rename quick_sort_3partition to quick_sort_3part --- sorts/quick_sort_3_partition.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sorts/quick_sort_3_partition.py b/sorts/quick_sort_3_partition.py index dee3f25359dc..9453d0316fce 100644 --- a/sorts/quick_sort_3_partition.py +++ b/sorts/quick_sort_3_partition.py @@ -18,25 +18,25 @@ def quick_sort_3partition(sorting: list, left: int, right: int) -> list: quick_sort_3partition(sorting, b + 1, right) -def quick_sort_3partition(sorting: list) -> list: +def quick_sort_3part(sorting: list) -> list: """ Another quick sort algorithm, returns a new sorted list - >>> quick_sort_3partition([]) + >>> quick_sort_3part([]) [] - >>> quick_sort_3partition([1]) + >>> quick_sort_3part([1]) [1] - >>> quick_sort_3partition([-5, -2, 1, -2, 0, 1]) + >>> quick_sort_3part([-5, -2, 1, -2, 0, 1]) [-5, -2, -2, 0, 1, 1] - >>> quick_sort_3partition([1, 2, 5, 1, 2, 0, 0, 5, 2, -1]) + >>> quick_sort_3part([1, 2, 5, 1, 2, 0, 0, 5, 2, -1]) [-1, 0, 0, 1, 1, 2, 2, 2, 5, 5] """ if len(sorting) <= 1: return sorting return ( - quick_sort_3partition([i for i in sorting if i < sorting[0]]) + quick_sort_3part([i for i in sorting if i < sorting[0]]) + [i for i in sorting if i == sorting[0]] - + quick_sort_3partition([i for i in sorting if i > sorting[0]]) + + quick_sort_3part([i for i in sorting if i > sorting[0]]) ) From 1a983e3c01c13864d88d00db74983eaa76fa660e Mon Sep 17 00:00:00 2001 From: Lewis Tian Date: Tue, 29 Sep 2020 15:30:11 +0800 Subject: [PATCH 3/4] Update sorts/quick_sort_3_partition.py rename quick_sort_3part to three_way_radix_quicksort Three-way radix quicksort: https://en.wikipedia.org/wiki/Quicksort#Three-way_radix_quicksort First divide the list into three parts. Then recursively sort the "less than" and "greater than" partitions. --- sorts/quick_sort_3_partition.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/sorts/quick_sort_3_partition.py b/sorts/quick_sort_3_partition.py index 9453d0316fce..604464f60e26 100644 --- a/sorts/quick_sort_3_partition.py +++ b/sorts/quick_sort_3_partition.py @@ -1,4 +1,4 @@ -def quick_sort_3partition(sorting: list, left: int, right: int) -> list: +def quick_sort_3partition(sorting: list, left: int, right: int): if right <= left: return a = i = left @@ -18,25 +18,28 @@ def quick_sort_3partition(sorting: list, left: int, right: int) -> list: quick_sort_3partition(sorting, b + 1, right) -def quick_sort_3part(sorting: list) -> list: +def three_way_radix_quicksort(sorting: list) -> list: """ - Another quick sort algorithm, returns a new sorted list + Three-way radix quicksort: + https://en.wikipedia.org/wiki/Quicksort#Three-way_radix_quicksort + First divide the list into three parts. + Then recursively sort the "less than" and "greater than" partitions. - >>> quick_sort_3part([]) + >>> three_way_radix_quicksort([]) [] - >>> quick_sort_3part([1]) + >>> three_way_radix_quicksort([1]) [1] - >>> quick_sort_3part([-5, -2, 1, -2, 0, 1]) + >>> three_way_radix_quicksort([-5, -2, 1, -2, 0, 1]) [-5, -2, -2, 0, 1, 1] - >>> quick_sort_3part([1, 2, 5, 1, 2, 0, 0, 5, 2, -1]) + >>> three_way_radix_quicksort([1, 2, 5, 1, 2, 0, 0, 5, 2, -1]) [-1, 0, 0, 1, 1, 2, 2, 2, 5, 5] """ if len(sorting) <= 1: return sorting return ( - quick_sort_3part([i for i in sorting if i < sorting[0]]) + three_way_radix_quicksort([i for i in sorting if i < sorting[0]]) + [i for i in sorting if i == sorting[0]] - + quick_sort_3part([i for i in sorting if i > sorting[0]]) + + three_way_radix_quicksort([i for i in sorting if i > sorting[0]]) ) From 38f2fc30d2831c74cd0afc30c0273770a6661fe8 Mon Sep 17 00:00:00 2001 From: Lewis Tian Date: Tue, 29 Sep 2020 19:13:23 +0800 Subject: [PATCH 4/4] Update sorts/quick_sort_3_partition.py Co-authored-by: Christian Clauss --- sorts/quick_sort_3_partition.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/quick_sort_3_partition.py b/sorts/quick_sort_3_partition.py index 604464f60e26..18c6e0f876d2 100644 --- a/sorts/quick_sort_3_partition.py +++ b/sorts/quick_sort_3_partition.py @@ -1,4 +1,4 @@ -def quick_sort_3partition(sorting: list, left: int, right: int): +def quick_sort_3partition(sorting: list, left: int, right: int) -> None: if right <= left: return a = i = left