From 31a3bf388ae8d1ce4908da62f3597a354682d00b Mon Sep 17 00:00:00 2001 From: Shweta Singh <95102195+Shwetasingh77@users.noreply.github.com> Date: Tue, 8 Oct 2024 18:52:38 +0530 Subject: [PATCH 1/7] Create reversesort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Time Complexity: The time complexity of this algorithm is still O(n²), similar to traditional selection sort, since it involves finding the minimum element in the unsorted portion and reversing a subarray. Unique Feature: The reversal of subarrays distinguishes this algorithm from traditional selection sort, providing an interesting approach to sorting. --- data_structures/reversesort | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 data_structures/reversesort diff --git a/data_structures/reversesort b/data_structures/reversesort new file mode 100644 index 000000000000..47a1baca3cf7 --- /dev/null +++ b/data_structures/reversesort @@ -0,0 +1,29 @@ +def reverse_selection_sort(arr): + n = len(arr) + + # Iterate over each position of the array + for i in range(n - 1): + # Find the index of the minimum element in the unsorted portion + min_index = i + for j in range(i + 1, n): + if arr[j] < arr[min_index]: + min_index = j + + # If the minimum is not already at position i, reverse the subarray + if min_index != i: + # Reverse the subarray from position i to min_index + arr[i:min_index + 1] = reversed(arr[i:min_index + 1]) + + return arr + +# Example usage: +arr = [64, 25, 12, 22, 11] +sorted_arr = reverse_selection_sort(arr) +print("Sorted array:", sorted_arr) + +# Sorted array: [11, 12, 22, 25, 64] + +# Explanation: +# Find the Minimum: For each position i in the array, find the minimum element in the unsorted portion. +# Reverse Subarray: After finding the minimum element, reverse the subarray starting from i up to the position of the minimum element (min_index). +# Repeat: This process repeats for each subsequent position, progressively sorting the array. From e6dca30986e31b8fd6a809658d2f30ede2db26aa Mon Sep 17 00:00:00 2001 From: Shweta Singh <95102195+Shwetasingh77@users.noreply.github.com> Date: Thu, 10 Oct 2024 01:49:18 +0530 Subject: [PATCH 2/7] Rename reversesort to reversesort.py --- data_structures/{reversesort => reversesort.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/{reversesort => reversesort.py} (100%) diff --git a/data_structures/reversesort b/data_structures/reversesort.py similarity index 100% rename from data_structures/reversesort rename to data_structures/reversesort.py From c376bbaf22a5894351a728e88c6470d1d5d890da Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:20:15 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/reversesort.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/reversesort.py b/data_structures/reversesort.py index 47a1baca3cf7..dbc3a14e6454 100644 --- a/data_structures/reversesort.py +++ b/data_structures/reversesort.py @@ -12,10 +12,11 @@ def reverse_selection_sort(arr): # If the minimum is not already at position i, reverse the subarray if min_index != i: # Reverse the subarray from position i to min_index - arr[i:min_index + 1] = reversed(arr[i:min_index + 1]) + arr[i : min_index + 1] = reversed(arr[i : min_index + 1]) return arr + # Example usage: arr = [64, 25, 12, 22, 11] sorted_arr = reverse_selection_sort(arr) From a3e3e3dc1b804dc3d2bfc6eb290b607a920f3c32 Mon Sep 17 00:00:00 2001 From: Shweta Singh <95102195+Shwetasingh77@users.noreply.github.com> Date: Thu, 10 Oct 2024 02:02:26 +0530 Subject: [PATCH 4/7] Update reversesort.py --- data_structures/reversesort.py | 48 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/data_structures/reversesort.py b/data_structures/reversesort.py index dbc3a14e6454..99bd470b65e4 100644 --- a/data_structures/reversesort.py +++ b/data_structures/reversesort.py @@ -1,4 +1,33 @@ -def reverse_selection_sort(arr): +from typing import List + +def reverse_selection_sort(arr: List[int]) -> List[int]: + """ + Sorts an array using a modified selection sort algorithm where after finding + the minimum element, a subarray is reversed instead of swapping. + + Parameters: + arr (List[int]): The list of integers to sort. + + Returns: + List[int]: The sorted list of integers. + + Example: + >>> reverse_selection_sort([64, 25, 12, 22, 11]) + [11, 12, 22, 25, 64] + + >>> reverse_selection_sort([5, 4, 3, 2, 1]) + [1, 2, 3, 4, 5] + + >>> reverse_selection_sort([3, 1, 2]) + [1, 2, 3] + + >>> reverse_selection_sort([10]) + [10] + + >>> reverse_selection_sort([]) + [] + """ + n = len(arr) # Iterate over each position of the array @@ -12,19 +41,10 @@ def reverse_selection_sort(arr): # If the minimum is not already at position i, reverse the subarray if min_index != i: # Reverse the subarray from position i to min_index - arr[i : min_index + 1] = reversed(arr[i : min_index + 1]) + arr[i:min_index + 1] = reversed(arr[i:min_index + 1]) return arr - -# Example usage: -arr = [64, 25, 12, 22, 11] -sorted_arr = reverse_selection_sort(arr) -print("Sorted array:", sorted_arr) - -# Sorted array: [11, 12, 22, 25, 64] - -# Explanation: -# Find the Minimum: For each position i in the array, find the minimum element in the unsorted portion. -# Reverse Subarray: After finding the minimum element, reverse the subarray starting from i up to the position of the minimum element (min_index). -# Repeat: This process repeats for each subsequent position, progressively sorting the array. +if __name__ == "__main__": + import doctest + doctest.testmod() From ee58b5960af0d24da7fe515b28a6876a62fe4c06 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:33:50 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/reversesort.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/data_structures/reversesort.py b/data_structures/reversesort.py index 99bd470b65e4..022e212c13ea 100644 --- a/data_structures/reversesort.py +++ b/data_structures/reversesort.py @@ -1,8 +1,9 @@ from typing import List + def reverse_selection_sort(arr: List[int]) -> List[int]: """ - Sorts an array using a modified selection sort algorithm where after finding + Sorts an array using a modified selection sort algorithm where after finding the minimum element, a subarray is reversed instead of swapping. Parameters: @@ -14,16 +15,16 @@ def reverse_selection_sort(arr: List[int]) -> List[int]: Example: >>> reverse_selection_sort([64, 25, 12, 22, 11]) [11, 12, 22, 25, 64] - + >>> reverse_selection_sort([5, 4, 3, 2, 1]) [1, 2, 3, 4, 5] - + >>> reverse_selection_sort([3, 1, 2]) [1, 2, 3] - + >>> reverse_selection_sort([10]) [10] - + >>> reverse_selection_sort([]) [] """ @@ -41,10 +42,12 @@ def reverse_selection_sort(arr: List[int]) -> List[int]: # If the minimum is not already at position i, reverse the subarray if min_index != i: # Reverse the subarray from position i to min_index - arr[i:min_index + 1] = reversed(arr[i:min_index + 1]) + arr[i : min_index + 1] = reversed(arr[i : min_index + 1]) return arr + if __name__ == "__main__": import doctest + doctest.testmod() From 720caf7f6388ed8abd24a093d3165c5250c066e4 Mon Sep 17 00:00:00 2001 From: Shweta Singh <95102195+Shwetasingh77@users.noreply.github.com> Date: Thu, 10 Oct 2024 02:09:18 +0530 Subject: [PATCH 6/7] Update reversesort.py --- data_structures/reversesort.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/data_structures/reversesort.py b/data_structures/reversesort.py index 022e212c13ea..74a04e676a70 100644 --- a/data_structures/reversesort.py +++ b/data_structures/reversesort.py @@ -1,30 +1,27 @@ -from typing import List - - -def reverse_selection_sort(arr: List[int]) -> List[int]: +def reverse_selection_sort(arr: list[int]) -> list[int]: """ - Sorts an array using a modified selection sort algorithm where after finding + Sorts an array using a modified selection sort algorithm where after finding the minimum element, a subarray is reversed instead of swapping. Parameters: - arr (List[int]): The list of integers to sort. + arr (list[int]): The list of integers to sort. Returns: - List[int]: The sorted list of integers. + list[int]: The sorted list of integers. Example: >>> reverse_selection_sort([64, 25, 12, 22, 11]) [11, 12, 22, 25, 64] - + >>> reverse_selection_sort([5, 4, 3, 2, 1]) [1, 2, 3, 4, 5] - + >>> reverse_selection_sort([3, 1, 2]) [1, 2, 3] - + >>> reverse_selection_sort([10]) [10] - + >>> reverse_selection_sort([]) [] """ @@ -42,12 +39,10 @@ def reverse_selection_sort(arr: List[int]) -> List[int]: # If the minimum is not already at position i, reverse the subarray if min_index != i: # Reverse the subarray from position i to min_index - arr[i : min_index + 1] = reversed(arr[i : min_index + 1]) + arr[i:min_index + 1] = reversed(arr[i:min_index + 1]) return arr - if __name__ == "__main__": import doctest - doctest.testmod() From e7351020cec027e21bfe07be7efa60b79a99badd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:39:39 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/reversesort.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/data_structures/reversesort.py b/data_structures/reversesort.py index 74a04e676a70..b59070a5b89e 100644 --- a/data_structures/reversesort.py +++ b/data_structures/reversesort.py @@ -1,6 +1,6 @@ def reverse_selection_sort(arr: list[int]) -> list[int]: """ - Sorts an array using a modified selection sort algorithm where after finding + Sorts an array using a modified selection sort algorithm where after finding the minimum element, a subarray is reversed instead of swapping. Parameters: @@ -12,16 +12,16 @@ def reverse_selection_sort(arr: list[int]) -> list[int]: Example: >>> reverse_selection_sort([64, 25, 12, 22, 11]) [11, 12, 22, 25, 64] - + >>> reverse_selection_sort([5, 4, 3, 2, 1]) [1, 2, 3, 4, 5] - + >>> reverse_selection_sort([3, 1, 2]) [1, 2, 3] - + >>> reverse_selection_sort([10]) [10] - + >>> reverse_selection_sort([]) [] """ @@ -39,10 +39,12 @@ def reverse_selection_sort(arr: list[int]) -> list[int]: # If the minimum is not already at position i, reverse the subarray if min_index != i: # Reverse the subarray from position i to min_index - arr[i:min_index + 1] = reversed(arr[i:min_index + 1]) + arr[i : min_index + 1] = reversed(arr[i : min_index + 1]) return arr + if __name__ == "__main__": import doctest + doctest.testmod()