Implementation of shell sort algorithm in Python :param collection: Some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending >>> shell_sort([3, 2, 1]) [1, 2, 3] >>> shell_sort([]) [] >>> shell_sort
(collection: list)
| 22 | |
| 23 | |
| 24 | def shell_sort(collection: list) -> list: |
| 25 | """Implementation of shell sort algorithm in Python |
| 26 | :param collection: Some mutable ordered collection with heterogeneous |
| 27 | comparable items inside |
| 28 | :return: the same collection ordered by ascending |
| 29 | |
| 30 | >>> shell_sort([3, 2, 1]) |
| 31 | [1, 2, 3] |
| 32 | >>> shell_sort([]) |
| 33 | [] |
| 34 | >>> shell_sort([1]) |
| 35 | [1] |
| 36 | """ |
| 37 | |
| 38 | # Choose an initial gap value |
| 39 | gap = len(collection) |
| 40 | |
| 41 | # Set the gap value to be decreased by a factor of 1.3 |
| 42 | # after each iteration |
| 43 | shrink = 1.3 |
| 44 | |
| 45 | # Continue sorting until the gap is 1 |
| 46 | while gap > 1: |
| 47 | # Decrease the gap value |
| 48 | gap = int(gap / shrink) |
| 49 | |
| 50 | # Sort the elements using insertion sort |
| 51 | for i in range(gap, len(collection)): |
| 52 | temp = collection[i] |
| 53 | j = i |
| 54 | while j >= gap and collection[j - gap] > temp: |
| 55 | collection[j] = collection[j - gap] |
| 56 | j -= gap |
| 57 | collection[j] = temp |
| 58 | |
| 59 | return collection |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected