MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / merge_sort

Function merge_sort

sorts/unknown_sort.py:9–34  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

7
8
9def 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
37if __name__ == "__main__":

Callers 1

unknown_sort.pyFile · 0.70

Calls 3

reverseMethod · 0.80
appendMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected