Algorithm Design & Analysis
CSE 2103
Lecture 5
Merge Sort
2
⚫ A sort algorithm that splits the items to be sorted
into two groups, recursively sorts each group, and
merges them into a final, sorted sequence.
⚫ Two functions are involved in this algorithm.
⚪ The MergeSort() function recursively calls itself to divide the
array till size becomes one.
⚪ The Merge() function is used for merging two halves.
Example
3
Example
4
⚫ Problem : Consider the array of elements a[1:10] =
(310, 285, 179, 652, 351, 423, 861, 254, 450, 520).
Solution :
1. (310, 285, 179, 652, 351 || 423, 861, 254, 450, 520)
2. (310, 285, 179 || 652, 351 || 423, 861, 254, 450, 520)
3. (310, 285 || 179 || 652, 351 || 423, 861, 254, 450, 520)
4. (310 || 285 || 179 || 652, 351 || 423, 861, 254, 450, 520)
5. (285, 310 || 179 || 652, 351 || 423, 861, 254, 450, 520)
6. (179, 285, 310 || 652, 351 || 423, 861, 254, 450, 520)
7. (179, 285, 310 || 351, 652 || 423, 861, 254, 450, 520)
8. (179, 285, 310, 351, 652 || 423, 861, 254, 450, 520)
Example
5
9. (179, 285, 310, 351, 652 || 423, 861, 254 || 450, 520)
10. (179, 285, 310, 351, 652 || 423, 861 || 254 || 450, 520)
11. (179, 285, 310, 351, 652 || 423 || 861 || 254 || 450, 520)
12. (179, 285, 310, 351, 652 || 423, 861 || 254 || 450, 520)
13. (179, 285, 310, 351, 652 || 254, 423, 861 || 450, 520)
14. (179, 285, 310, 351, 652 || 254, 423, 450, 520, 861)
15. (179, 254, 285, 310, 351, 423, 450, 520, 652, 861)
Tree of calls of Merge Sort (1,10)
6
Merge Sort Algorithm
7
Merge Sort Algorithm
8
9
THANK YOU