Merge sort works as follows:
procedure merge_sort(array : list of sortable items, start : first element of list, end : last element of list) if start < end mid ← (start + end) / 2 merge_sort(array, start, mid) merge_sort(array, mid + 1, end) merge(array, start, mid, end) end if end procedure
procedure merge(array : list of items to merge, start : first element of first sublist, mid : last element of first sublist, end : last element of second sublist) temp : temporary array of size (end - start + 1) i ← start j ← mid + 1 k ← 0 while i <= mid and j <= end if array[i] < array[j] temp[k] ← array[i] i ← i + 1 else temp[k] ← array[j] j ← j + 1 end if k ← k + 1 end while while i <= mid temp[k] ← array[i] i ← i + 1 k ← k + 1 end while while j <= end temp[k] ← array[j] j ← j + 1 k ← k + 1 end while Copy elements of temp back into array end procedure
In this example, the merge sort merge()
function is called with the arguments array
(the array to partition), 0 (the subscript of the first element of the array), and 11 (the subscript of the last element of the array).
Begin merge
i ← start j ← mid + 1 k ← 0
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
||||||||||||
k
|
i <= mid and j <= end; loop entered. array[i] < array[j]
temp[k] ← array[i] i ← i + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | |||||||||||
k
|
i <= mid and j <= end; loop continues. array[i] not < array[j]
temp[k] ← array[j] j ← j + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | ||||||||||
k
|
i <= mid and j <= end; loop continues. array[i] < array[j]
temp[k] ← array[i] i ← i + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | |||||||||
k
|
i <= mid and j <= end; loop continues. array[i] < array[j]
temp[k] ← array[i] i ← i + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | ||||||||
k
|
i <= mid and j <= end; loop continues. array[i] not < array[j]
temp[k] ← array[j] j ← j + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | |||||||
k
|
i <= mid and j <= end; loop continues. array[i] < array[j]
temp[k] ← array[i] i ← i + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | 48 | ||||||
k
|
i <= mid and j <= end; loop continues. array[i] not < array[j]
temp[k] ← array[j] j ← j + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
i
|
mid
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | 48 | 58 | |||||
k
|
i <= mid and j <= end; loop continues. array[i] < array[j]
temp[k] ← array[i] i ← i + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
mid i
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | 48 | 58 | 67 | ||||
k
|
i <= mid and j <= end; loop continues. array[i] < not array[j]
temp[k] ← array[j] j ← j + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
mid i
|
j
|
end
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | 48 | 58 | 67 | 72 | |||
k
|
i <= mid and j <= end; loop continues. array[i] < not array[j]
temp[k] ← array[j] j ← j + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
mid i
|
end j
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | 48 | 58 | 67 | 72 | 79 | ||
k
|
i <= mid and j <= end; loop continues. array[i] < array[j]
temp[k] ← array[i] i ← i + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 |
start
|
mid |
i
|
end j
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | |
temp
|
13 | 14 | 22 | 31 | 45 | 48 | 58 | 67 | 72 | 79 | 81 | |
k
|
i not <= mid; loop ends. i not <= mid; loop not entered. j <= end; loop entered.
temp[k] ← array[j] j ← j + 1 k ← k + 1
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | ||
array
|
13 | 22 | 31 | 48 | 67 | 81 | 14 | 45 | 58 | 72 | 79 | 93 | |
start
|
mid i
|
end
|
j
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] | [11] | ||
temp
|
13 | 14 | 22 | 31 | 45 | 48 | 58 | 67 | 72 | 79 | 81 | 93 | |
k
|
Time Complexity: O(n log n)
Space Complexity: O(n)