Professional Documents
Culture Documents
L4
L4
Result:
Result: 1
Result: 1 3
Result: 1 3 5
Result: 1 3 5 10
Result: 1 3 5 10 23
Result: 1 3 5 10 23 25
Result: 1 3 5 10 23 25 54
Result: 1 3 5 10 23 25 54 75
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
4 0
04/18/24 Principles of Computer Algorithms 18
99 6 86 15 58 35 86 0 4
Merge 4 0
04/18/24 Principles of Computer Algorithms 19
6 99 15 86 58 35 0 4 86
99 6 86 15 58 35 86 0 4
Merge
04/18/24 Principles of Computer Algorithms 20
6 15 86 99 0 4 35 58 86
6 99 15 86 58 35 0 4 86
Merge
04/18/24 Principles of Computer Algorithms 21
0 4 6 15 35 58 86 86 99
6 15 86 99 0 4 35 58 86
Merge
04/18/24 Principles of Computer Algorithms 22
0 4 6 15 35 58 86 86 99
Merge
1. n1 ← q − p + 1
2. n2 ← r − q
3. Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]
4. FOR i ← 1 TO n1
5. DO L[i] ← A[p + i − 1]
6. FOR j ← 1 TO n2
7. DO R[j] ← A[q + j ]
8. L[n1 + 1] ← ∞
9. R[n2 + 1] ← ∞
10. i←1
11. j←1
12. FOR k ← p TO r
13. DO IF L[i ] ≤ R[ j]
14. THEN A[k] ← L[i]
There are other variants of Merge Sorts including k-
way merge sorting, but the common variant is the Double
Memory Merge Sort. Though the running time is O(N logN)
and runs much faster than insertion sort and bubble sort,
merge sort’s large memory demands makes it not very
practical for main memory sorting.