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

Function merge

Sorting Algorithms/Merge Sort.py:8–49  ·  view source on GitHub ↗
(arr, l, m, r)

Source from the content-addressed store, hash-verified

6
7
8def merge(arr, l, m, r):
9 n1 = m - l + 1
10 n2 = r - m
11
12 # create temp arrays
13 L = [0] * (n1)
14 R = [0] * (n2)
15
16 # Copy data to temp arrays L[] and R[]
17 for i in range(0, n1):
18 L[i] = arr[l + i]
19
20 for j in range(0, n2):
21 R[j] = arr[m + 1 + j]
22
23 # Merge the temp arrays back into arr[l..r]
24 i = 0 # Initial index of first subarray
25 j = 0 # Initial index of second subarray
26 k = l # Initial index of merged subarray
27
28 while i < n1 and j < n2:
29 if L[i] <= R[j]:
30 arr[k] = L[i]
31 i += 1
32 else:
33 arr[k] = R[j]
34 j += 1
35 k += 1
36
37 # Copy the remaining elements of L[], if there
38 # are any
39 while i < n1:
40 arr[k] = L[i]
41 i += 1
42 k += 1
43
44 # Copy the remaining elements of R[], if there
45 # are any
46 while j < n2:
47 arr[k] = R[j]
48 j += 1
49 k += 1
50
51
52# l is for left index and r is right index of the

Callers 1

mergeSortFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected