From 37569235c8f0e1786615a7b77f629cac2c3a9dd8 Mon Sep 17 00:00:00 2001 From: kevinzb56 <143kevinshah@gmail.com> Date: Sat, 12 Oct 2024 18:43:28 +0530 Subject: [PATCH 1/2] Added missing Reverse Selection Sort --- sorts/reverse_selection_sort.py | 42 +++++++++++++++++++++++++++++++++ tempCodeRunnerFile.python | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 sorts/reverse_selection_sort.py create mode 100644 tempCodeRunnerFile.python diff --git a/sorts/reverse_selection_sort.py b/sorts/reverse_selection_sort.py new file mode 100644 index 000000000000..2d4af695de7a --- /dev/null +++ b/sorts/reverse_selection_sort.py @@ -0,0 +1,42 @@ +""" +A pure Python implementation of the Reverse Selection Sort algorithm. +This algorithm progressively sorts the array by finding the largest +element in each pass and placing it at the end of the sorted portion. +For doctests run the following command: +python3 -m doctest -v reverse_selection_sort.py +For manual testing run: +python3 reverse_selection_sort.py +""" + +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 descending order + Examples: + >>> reverse_selection_sort([45,23,11,89,54,1,3,36]) + [89, 54, 45, 36, 23, 11, 3, 1] + >>> reverse_selection_sort([0,-89,32,5,46,8,11]) + [46, 32, 11, 8, 5, 0, -89] + >>> reverse_selection_sort([34,-2,-1,98,-42]) + [98, 34, -1, -2, -42] + """ + n = len(collection) + for i in range(n): + # Find the maximum element in the unsorted portion + max_idx = i + for j in range(i + 1, n): + if collection[j] > collection[max_idx]: + max_idx = j + + # Reverse the subarray from the current position to the end + collection[i:max_idx+1] = reversed(collection[i:max_idx+1]) + + 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)) diff --git a/tempCodeRunnerFile.python b/tempCodeRunnerFile.python new file mode 100644 index 000000000000..2d4af695de7a --- /dev/null +++ b/tempCodeRunnerFile.python @@ -0,0 +1,42 @@ +""" +A pure Python implementation of the Reverse Selection Sort algorithm. +This algorithm progressively sorts the array by finding the largest +element in each pass and placing it at the end of the sorted portion. +For doctests run the following command: +python3 -m doctest -v reverse_selection_sort.py +For manual testing run: +python3 reverse_selection_sort.py +""" + +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 descending order + Examples: + >>> reverse_selection_sort([45,23,11,89,54,1,3,36]) + [89, 54, 45, 36, 23, 11, 3, 1] + >>> reverse_selection_sort([0,-89,32,5,46,8,11]) + [46, 32, 11, 8, 5, 0, -89] + >>> reverse_selection_sort([34,-2,-1,98,-42]) + [98, 34, -1, -2, -42] + """ + n = len(collection) + for i in range(n): + # Find the maximum element in the unsorted portion + max_idx = i + for j in range(i + 1, n): + if collection[j] > collection[max_idx]: + max_idx = j + + # Reverse the subarray from the current position to the end + collection[i:max_idx+1] = reversed(collection[i:max_idx+1]) + + 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 5cdc818532477f9c50497fd6681895b1b0430865 Mon Sep 17 00:00:00 2001 From: kevinzb56 <143kevinshah@gmail.com> Date: Sat, 12 Oct 2024 19:17:34 +0530 Subject: [PATCH 2/2] Added Reverse Selection Sort --- sorts/reverse_selection_sort.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/sorts/reverse_selection_sort.py b/sorts/reverse_selection_sort.py index 2d4af695de7a..aeb5cf7cf6d6 100644 --- a/sorts/reverse_selection_sort.py +++ b/sorts/reverse_selection_sort.py @@ -1,26 +1,27 @@ """ -A pure Python implementation of the Reverse Selection Sort algorithm. +A Python implementation of the Reverse Selection Sort algorithm. + This algorithm progressively sorts the array by finding the largest -element in each pass and placing it at the end of the sorted portion. -For doctests run the following command: -python3 -m doctest -v reverse_selection_sort.py -For manual testing run: -python3 reverse_selection_sort.py +element and reversing subarrays to place it at the correct position. + +Inspired by the Pancake Sorting algorithm. +For more information, see: https://en.wikipedia.org/wiki/Pancake_sorting + +Examples: +>>> reverse_selection_sort([45, 23, 11, 89, 54, 1, 3, 36]) +[89, 54, 45, 36, 23, 11, 3, 1] +>>> reverse_selection_sort([0, -89, 32, 5, 46, 8, 11]) +[46, 32, 11, 8, 5, 0, -89] +>>> reverse_selection_sort([34, -2, -1, 98, -42]) +[98, 34, -1, -2, -42] """ def reverse_selection_sort(collection: list) -> list: """ - A pure implementation of reverse selection sort algorithm in Python + 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 descending order - Examples: - >>> reverse_selection_sort([45,23,11,89,54,1,3,36]) - [89, 54, 45, 36, 23, 11, 3, 1] - >>> reverse_selection_sort([0,-89,32,5,46,8,11]) - [46, 32, 11, 8, 5, 0, -89] - >>> reverse_selection_sort([34,-2,-1,98,-42]) - [98, 34, -1, -2, -42] """ n = len(collection) for i in range(n):