Asked in
Computer Programming

What is the pseudocode for merge sort?

Answer

User Avatar
Wiki User
April 26, 2015 9:24PM

The pseudocode for merge sort is:

MERGE (A, p, q, r )

1. n1 ← qp + 1 2. n2 ← rq

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 kp TO r

13. DO IF L[i ] ≤ R[ j]

14. THEN A[k] ← L[i]

15. ii + 1

16. ELSE A[k] ← R[j]

17. jj + 1