|
1 | | -from typing import Any |
2 | | - |
3 | | - |
4 | | -def double_sort(collection: list[Any]) -> list[Any]: |
5 | | - """This sorting algorithm sorts an array using the principle of bubble sort, |
6 | | - but does it both from left to right and right to left. |
7 | | - Hence, it's called "Double sort" |
8 | | - :param collection: mutable ordered sequence of elements |
9 | | - :return: the same collection in ascending order |
10 | | - Examples: |
11 | | - >>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6 ,-7]) |
12 | | - [-7, -6, -5, -4, -3, -2, -1] |
13 | | - >>> double_sort([]) |
14 | | - [] |
15 | | - >>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6]) |
16 | | - [-6, -5, -4, -3, -2, -1] |
17 | | - >>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29]) |
18 | | - True |
19 | | - """ |
20 | | - no_of_elements = len(collection) |
21 | | - for _ in range( |
22 | | - int(((no_of_elements - 1) / 2) + 1) |
23 | | - ): # we don't need to traverse to end of list as |
24 | | - for j in range(no_of_elements - 1): |
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 |
38 | | - |
39 | | - |
40 | | -if __name__ == "__main__": |
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] |
43 | | - print("the sorted list is") |
44 | | - print(f"{double_sort(unsorted) = }") |
| 1 | + |
| 2 | +from typing import Protocol |
| 3 | + |
| 4 | + |
| 5 | +class Comparable(Protocol): |
| 6 | + def __lt__(self, other: object, /) -> bool: ... |
| 7 | + |
| 8 | + |
| 9 | +def double_sort[T: Comparable](collection: list[T]) -> list[T]: |
| 10 | + """This sorting algorithm sorts an array using the principle of bubble sort, |
| 11 | + but does it both from left to right and right to left. |
| 12 | + Hence, it's called "Double sort" |
| 13 | + :param collection: mutable ordered sequence of elements |
| 14 | + :return: the same collection in ascending order |
| 15 | + Examples: |
| 16 | + >>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6 ,-7]) |
| 17 | + [-7, -6, -5, -4, -3, -2, -1] |
| 18 | + >>> double_sort([]) |
| 19 | + [] |
| 20 | + >>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6]) |
| 21 | + [-6, -5, -4, -3, -2, -1] |
| 22 | + >>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29]) |
| 23 | + True |
| 24 | +
|
| 25 | + >>> double_sort(["d", "a", "c", "b"]) |
| 26 | + ['a', 'b', 'c', 'd'] |
| 27 | + >>> double_sort([2.5, -1.0, 0.0]) |
| 28 | + [-1.0, 0.0, 2.5] |
| 29 | +""" |
| 30 | + no_of_elements = len(collection) |
| 31 | + for _ in range( |
| 32 | + int(((no_of_elements - 1) / 2) + 1) |
| 33 | + ): # we don't need to traverse to end of list as |
| 34 | + for j in range(no_of_elements - 1): |
| 35 | + # apply the bubble sort algorithm from left to right (or forwards) |
| 36 | + if collection[j + 1] < collection[j]: |
| 37 | + collection[j], collection[j + 1] = collection[j + 1], collection[j] |
| 38 | + # apply the bubble sort algorithm from right to left (or backwards) |
| 39 | + if collection[no_of_elements - 1 - j] < collection[no_of_elements - 2 - j]: |
| 40 | + ( |
| 41 | + collection[no_of_elements - 1 - j], |
| 42 | + collection[no_of_elements - 2 - j], |
| 43 | + ) = ( |
| 44 | + collection[no_of_elements - 2 - j], |
| 45 | + collection[no_of_elements - 1 - j], |
| 46 | + ) |
| 47 | + return collection |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + # allow the user to input the elements of the list on one line |
| 52 | + unsorted = [int(x) for x in input("Enter the list to be sorted: ").split() if x] |
| 53 | + print("the sorted list is") |
| 54 | + print(f"{double_sort(unsorted) = }") |
0 commit comments