MCPcopy Create free account
hub / github.com/geekcomputers/Python / merge

Function merge

Sorting Algorithms/Tim_sort.py:27–61  ·  view source on GitHub ↗
(arr, l, m, r)

Source from the content-addressed store, hash-verified

25
26# merge function merges the sorted runs
27def merge(arr, l, m, r):
28 # original array is broken in two parts
29 # left and right array
30 len1, len2 = m - l + 1, r - m
31 left, right = [], []
32 for i in range(0, len1):
33 left.append(arr[l + i])
34 for i in range(0, len2):
35 right.append(arr[m + 1 + i])
36
37 i, j, k = 0, 0, l
38 # after comparing, we merge those two array
39 # in larger sub array
40 while i < len1 and j < len2:
41 if left[i] <= right[j]:
42 arr[k] = left[i]
43 i += 1
44
45 else:
46 arr[k] = right[j]
47 j += 1
48
49 k += 1
50
51 # copy remaining elements of left, if any
52 while i < len1:
53 arr[k] = left[i]
54 k += 1
55 i += 1
56
57 # copy remaining element of right, if any
58 while j < len2:
59 arr[k] = right[j]
60 k += 1
61 j += 1
62
63
64# iterative Timsort function to sort the

Callers 1

timSortFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected