Skip to content

Commit 417b7ed

Browse files
Krishna-Singhalpre-commit-ci[bot]cclauss
authored
code enhancement in sort.double_sort (TheAlgorithms#10798)
* don't need to return list because list is mutable * Don't need to return list as list is mutable * use advantage of python in swapping * filter blank inputs from input list * minor changes * minor mistake * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more readable * Update double_sort.py * last fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent a8b6bda commit 417b7ed

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

sorts/double_sort.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def double_sort(lst):
1+
from typing import Any
2+
3+
4+
def double_sort(collection: list[Any]) -> list[Any]:
25
"""This sorting algorithm sorts an array using the principle of bubble sort,
36
but does it both from left to right and right to left.
47
Hence, it's called "Double sort"
@@ -14,29 +17,28 @@ def double_sort(lst):
1417
>>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29])
1518
True
1619
"""
17-
no_of_elements = len(lst)
20+
no_of_elements = len(collection)
1821
for _ in range(
1922
int(((no_of_elements - 1) / 2) + 1)
2023
): # we don't need to traverse to end of list as
2124
for j in range(no_of_elements - 1):
22-
if (
23-
lst[j + 1] < lst[j]
24-
): # applying bubble sort algorithm from left to right (or forwards)
25-
temp = lst[j + 1]
26-
lst[j + 1] = lst[j]
27-
lst[j] = temp
28-
if (
29-
lst[no_of_elements - 1 - j] < lst[no_of_elements - 2 - j]
30-
): # applying bubble sort algorithm from right to left (or backwards)
31-
temp = lst[no_of_elements - 1 - j]
32-
lst[no_of_elements - 1 - j] = lst[no_of_elements - 2 - j]
33-
lst[no_of_elements - 2 - j] = temp
34-
return lst
25+
# apply the bubble sort algorithm from left to right (or forwards)
26+
if collection[j + 1] < collection[j]:
27+
collection[j], collection[j + 1] = collection[j + 1], collection[j]
28+
# apply the bubble sort algorithm from right to left (or backwards)
29+
if collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j]:
30+
(
31+
collection[no_of_elements - 1 - j],
32+
collection[no_of_elements - 2 - j],
33+
) = (
34+
collection[no_of_elements - 2 - j],
35+
collection[no_of_elements - 1 - j],
36+
)
37+
return collection
3538

3639

3740
if __name__ == "__main__":
38-
print("enter the list to be sorted")
39-
lst = [int(x) for x in input().split()] # inputing elements of the list in one line
40-
sorted_lst = double_sort(lst)
41+
# allow the user to input the elements of the list on one line
42+
unsorted = [int(x) for x in input("Enter the list to be sorted: ").split() if x]
4143
print("the sorted list is")
42-
print(sorted_lst)
44+
print(f"{double_sort(unsorted) = }")

0 commit comments

Comments
 (0)