Skip to content
Closed
Changes from all commits
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
32 changes: 32 additions & 0 deletions sorts/ReverSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def reversort(collection: list[int]) -> list[int]:

Check failure on line 1 in sorts/ReverSort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

sorts/ReverSort.py:1:1: N999 Invalid module name: 'ReverSort'
"""
Sorts a list in ascending order using the Reversort algorithm.

:param collection: A list of integers to be sorted.
:return: The sorted list.

Examples:
>>> reversort([4, 2, 1, 3])
[1, 2, 3, 4]

>>> reversort([5, 6, 3, 2, 8])
[2, 3, 5, 6, 8]

>>> reversort([7])
[7]
"""

length = len(collection)
for i in range(length - 1):
# Find the index of the minimum element in the subarray collection[i:]
j = min(range(i, length), key=collection.__getitem__)
# Reverse the subarray collection[i:j+1]
collection[i:j + 1] = collection[i:j + 1][::-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(",")]
sorted_list = reversort(unsorted)
print("Sorted List:", sorted_list)
Loading