Skip to content

Update sorts/quick_sort_3_partition.py #2507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions sorts/quick_sort_3_partition.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick_sort_3partition does not return a new list, it just updates in place. So I remove -> list.

if right <= left:
return
a = i = left
Expand All @@ -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]])
)


Expand Down