From 96c5b777e8e95abeb904b6734583be9688154a40 Mon Sep 17 00:00:00 2001 From: Amarpreet Singh Date: Sat, 5 Oct 2024 23:30:06 -0700 Subject: [PATCH 1/4] Added reverse_selection.py --- sorts/reverse_selection.py | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 sorts/reverse_selection.py diff --git a/sorts/reverse_selection.py b/sorts/reverse_selection.py new file mode 100644 index 000000000000..12e253676d91 --- /dev/null +++ b/sorts/reverse_selection.py @@ -0,0 +1,62 @@ +""" +A pure Python implementation of the Reverse Selection Sort algorithm + +This algorithm progressively sorts the array by reversing subarrays + +For doctests run following command: +python3 -m doctest -v reverse_selection_sort.py + +For manual testing run: +python3 reverse_selection_sort.py +""" + +def reverse_subarray(arr: list, start: int, end: int) -> None: + """ + Reverse a subarray in-place. + + :param arr: the array containing the subarray to be reversed + :param start: the starting index of the subarray + :param end: the ending index of the subarray + """ + while start < end: + arr[start], arr[end] = arr[end], arr[start] + start += 1 + end -= 1 + +def reverse_selection_sort(collection: list) -> list: + """ + A pure implementation of reverse selection sort algorithm in Python + + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection sorted in ascending order + + Examples: + >>> reverse_selection_sort([1, 9, 5, 21, 17, 6]) + [1, 5, 6, 9, 17, 21] + + >>> reverse_selection_sort([]) + [] + + >>> reverse_selection_sort([-3, -17, -48]) + [-48, -17, -3] + """ + n = len(collection) + for i in range(n - 1): + # Find the minimum element in the unsorted portion + min_idx = i + for j in range(i + 1, n): + if collection[j] < collection[min_idx]: + min_idx = j + + # If the minimum is not at the start of the unsorted portion, + # reverse the subarray to bring it to the front + if min_idx != i: + reverse_subarray(collection, i, min_idx) + + return collection + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(reverse_selection_sort(unsorted)) From 608e6ed3c7f31aff7512c5cd0845f5915cb4eaa1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 06:36:10 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/reverse_selection.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sorts/reverse_selection.py b/sorts/reverse_selection.py index 12e253676d91..a472decea188 100644 --- a/sorts/reverse_selection.py +++ b/sorts/reverse_selection.py @@ -10,10 +10,11 @@ python3 reverse_selection_sort.py """ + def reverse_subarray(arr: list, start: int, end: int) -> None: """ Reverse a subarray in-place. - + :param arr: the array containing the subarray to be reversed :param start: the starting index of the subarray :param end: the ending index of the subarray @@ -23,6 +24,7 @@ def reverse_subarray(arr: list, start: int, end: int) -> None: start += 1 end -= 1 + def reverse_selection_sort(collection: list) -> list: """ A pure implementation of reverse selection sort algorithm in Python @@ -48,14 +50,15 @@ def reverse_selection_sort(collection: list) -> list: for j in range(i + 1, n): if collection[j] < collection[min_idx]: min_idx = j - + # If the minimum is not at the start of the unsorted portion, # reverse the subarray to bring it to the front if min_idx != i: reverse_subarray(collection, i, min_idx) - + return collection + if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From 7c59ca32437e61449cccd259769aad53d7325a1f Mon Sep 17 00:00:00 2001 From: Amarpreet Singh Date: Sat, 5 Oct 2024 23:38:07 -0700 Subject: [PATCH 3/4] Updated reverse_selection.py --- sorts/reverse_selection.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/sorts/reverse_selection.py b/sorts/reverse_selection.py index a472decea188..6c347a0f31d0 100644 --- a/sorts/reverse_selection.py +++ b/sorts/reverse_selection.py @@ -10,21 +10,35 @@ python3 reverse_selection_sort.py """ - def reverse_subarray(arr: list, start: int, end: int) -> None: """ Reverse a subarray in-place. - + :param arr: the array containing the subarray to be reversed :param start: the starting index of the subarray :param end: the ending index of the subarray + + Examples: + >>> lst = [1, 2, 3, 4, 5] + >>> reverse_subarray(lst, 1, 3) + >>> lst + [1, 4, 3, 2, 5] + + >>> lst = [1] + >>> reverse_subarray(lst, 0, 0) + >>> lst + [1] + + >>> lst = [1, 2] + >>> reverse_subarray(lst, 0, 1) + >>> lst + [2, 1] """ while start < end: arr[start], arr[end] = arr[end], arr[start] start += 1 end -= 1 - def reverse_selection_sort(collection: list) -> list: """ A pure implementation of reverse selection sort algorithm in Python @@ -42,6 +56,12 @@ def reverse_selection_sort(collection: list) -> list: >>> reverse_selection_sort([-3, -17, -48]) [-48, -17, -3] + + >>> reverse_selection_sort([1, 1, 1, 1]) + [1, 1, 1, 1] + + >>> reverse_selection_sort([5, 4, 3, 2, 1]) + [1, 2, 3, 4, 5] """ n = len(collection) for i in range(n - 1): @@ -50,15 +70,14 @@ def reverse_selection_sort(collection: list) -> list: for j in range(i + 1, n): if collection[j] < collection[min_idx]: min_idx = j - + # If the minimum is not at the start of the unsorted portion, # reverse the subarray to bring it to the front if min_idx != i: reverse_subarray(collection, i, min_idx) - + return collection - if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From 5c229b69fa74f0439a4feef9b004830a768e0100 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 06:38:29 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/reverse_selection.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sorts/reverse_selection.py b/sorts/reverse_selection.py index 6c347a0f31d0..5d11ec97bd52 100644 --- a/sorts/reverse_selection.py +++ b/sorts/reverse_selection.py @@ -10,10 +10,11 @@ python3 reverse_selection_sort.py """ + def reverse_subarray(arr: list, start: int, end: int) -> None: """ Reverse a subarray in-place. - + :param arr: the array containing the subarray to be reversed :param start: the starting index of the subarray :param end: the ending index of the subarray @@ -39,6 +40,7 @@ def reverse_subarray(arr: list, start: int, end: int) -> None: start += 1 end -= 1 + def reverse_selection_sort(collection: list) -> list: """ A pure implementation of reverse selection sort algorithm in Python @@ -70,14 +72,15 @@ def reverse_selection_sort(collection: list) -> list: for j in range(i + 1, n): if collection[j] < collection[min_idx]: min_idx = j - + # If the minimum is not at the start of the unsorted portion, # reverse the subarray to bring it to the front if min_idx != i: reverse_subarray(collection, i, min_idx) - + return collection + if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")]