Pure implementation of the fastest merge sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: a collection ordered by ascending Examples: >>> merge_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> merg
(collection: list)
| 7 | |
| 8 | |
| 9 | def merge_sort(collection: list) -> list: |
| 10 | """Pure implementation of the fastest merge sort algorithm in Python |
| 11 | |
| 12 | :param collection: some mutable ordered collection with heterogeneous |
| 13 | comparable items inside |
| 14 | :return: a collection ordered by ascending |
| 15 | |
| 16 | Examples: |
| 17 | >>> merge_sort([0, 5, 3, 2, 2]) |
| 18 | [0, 2, 2, 3, 5] |
| 19 | |
| 20 | >>> merge_sort([]) |
| 21 | [] |
| 22 | |
| 23 | >>> merge_sort([-2, -5, -45]) |
| 24 | [-45, -5, -2] |
| 25 | """ |
| 26 | start, end = [], [] |
| 27 | while len(collection) > 1: |
| 28 | min_one, max_one = min(collection), max(collection) |
| 29 | start.append(min_one) |
| 30 | end.append(max_one) |
| 31 | collection.remove(min_one) |
| 32 | collection.remove(max_one) |
| 33 | end.reverse() |
| 34 | return start + collection + end |
| 35 | |
| 36 | |
| 37 | if __name__ == "__main__": |
no test coverage detected