diff --git a/DIRECTORY.md b/DIRECTORY.md index fa46f377c278..e8c970627dcd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1354,11 +1354,13 @@ * [Ternary Search](searches/ternary_search.py) ## [Sorts](sorts) + * [Adaptive Merge Sort](sorts/adaptive_merge_sort.py) * [Bead Sort](sorts/bead_sort.py) * [Binary Insertion Sort](sorts/binary_insertion_sort.py) * [Bitonic Sort](sorts/bitonic_sort.py) * [Bogo Sort](sorts/bogo_sort.py) * [Bubble Sort](sorts/bubble_sort.py) + * [Bubble Sort Recursive](sorts/bubble_sort_recursive.py) * [Bucket Sort](sorts/bucket_sort.py) * [Circle Sort](sorts/circle_sort.py) * [Cocktail Shaker Sort](sorts/cocktail_shaker_sort.py) @@ -1376,6 +1378,7 @@ * [Insertion Sort](sorts/insertion_sort.py) * [Intro Sort](sorts/intro_sort.py) * [Iterative Merge Sort](sorts/iterative_merge_sort.py) + * [Kirkpatrick Reisch Sort](sorts/kirkpatrick_reisch_sort.py) * [Merge Insertion Sort](sorts/merge_insertion_sort.py) * [Merge Sort](sorts/merge_sort.py) * [Msd Radix Sort](sorts/msd_radix_sort.py) @@ -1387,16 +1390,20 @@ * [Patience Sort](sorts/patience_sort.py) * [Pigeon Sort](sorts/pigeon_sort.py) * [Pigeonhole Sort](sorts/pigeonhole_sort.py) + * [Power Sort](sorts/power_sort.py) * [Quick Sort](sorts/quick_sort.py) * [Quick Sort 3 Partition](sorts/quick_sort_3_partition.py) * [Radix Sort](sorts/radix_sort.py) * [Recursive Insertion Sort](sorts/recursive_insertion_sort.py) * [Recursive Mergesort Array](sorts/recursive_mergesort_array.py) * [Recursive Quick Sort](sorts/recursive_quick_sort.py) + * [Reverse Selection](sorts/reverse_selection.py) + * [Reversort](sorts/reversort.py) * [Selection Sort](sorts/selection_sort.py) * [Shell Sort](sorts/shell_sort.py) * [Shrink Shell Sort](sorts/shrink_shell_sort.py) * [Slowsort](sorts/slowsort.py) + * [Smoothsort](sorts/smoothsort.py) * [Stalin Sort](sorts/stalin_sort.py) * [Stooge Sort](sorts/stooge_sort.py) * [Strand Sort](sorts/strand_sort.py) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index 11c202788035..d960c8d7c20b 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -1,21 +1,27 @@ """ -This is a pure Python implementation of the merge sort algorithm. +The merge sort algorithm. -For doctests run following command: +For doctests, run the following command: python -m doctest -v merge_sort.py or python3 -m doctest -v merge_sort.py -For manual testing run: +For manual testing, run: python merge_sort.py """ +from typing import Protocol -def merge_sort(collection: list) -> list: + +class Comparable(Protocol): + def __lt__(self, other: object, /) -> bool: ... + + +def merge_sort[T: Comparable](collection: list[T]) -> list[T]: """ Sorts a list using the merge sort algorithm. - :param collection: A mutable ordered collection with comparable items. - :return: The same collection ordered in ascending order. + :param collection: A collection with comparable items. + :return: The collection ordered in ascending order. Time Complexity: O(n log n) Space Complexity: O(n) @@ -23,13 +29,15 @@ def merge_sort(collection: list) -> list: Examples: >>> merge_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] + >>> merge_sort([]) [] - >>> merge_sort([-2, -5, -45]) + + >>> merge_sort([-2, -45, -5]) [-45, -5, -2] """ - def merge(left: list, right: list) -> list: + def merge(left: list[T], right: list[T]) -> list[T]: """ Merge two sorted lists into a single sorted list. @@ -37,9 +45,12 @@ def merge(left: list, right: list) -> list: :param right: Right collection :return: Merged result """ - result = [] + result: list[T] = [] while left and right: - result.append(left.pop(0) if left[0] <= right[0] else right.pop(0)) + if right[0] < left[0]: + result.append(right.pop(0)) + else: + result.append(left.pop(0)) result.extend(left) result.extend(right) return result