Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

# Merge sort

• 1,300 views

More in: Technology
• Comment goes here.
Are you sure you want to
Be the first to comment
Be the first to like this

Total Views
1,300
On Slideshare
0
From Embeds
0
Number of Embeds
0

Shares
55
0
Likes
0

No embeds

### Report content

No notes for slide

### Transcript

• 1. Sorting a Sequence With Merge Sort
• Sort the sequence 6, 5, 8, 3, 2, 7, 1 using merge sort
• 2. Sorting a Sequence With Merge Sort
• Split the sequence at m =  ( i + j )/2 
• i =1, j =7, m =4
1 7 2 3 8 5 6
• 3. Sorting a Sequence With Merge Sort
• Make a recursive call to merge sort
• m :=  ( i + j )/2  =  2.5  =2
1 7 2 3 8 5 6
• 4. Sorting a Sequence With Merge Sort
• Now sorting this part
• i =1, j =2, m =1 and make a recursive call
1 7 2 3 8 5 6
• 5. Sorting a Sequence With Merge Sort
• Sequence of length 1 is already sorted, so return
1 7 2 3 8 5 6
• 6. Sorting a Sequence With Merge Sort
• Sequence of length 1 is sorted
1 7 2 3 8 5 6
• 7. Sorting a Sequence With Merge Sort
• Return control and merge these 2 sub-sequences into correct order
1 7 2 3 8 5 6
• 8. Sorting a Sequence With Merge Sort
• Now sort the second part of this subsequence
1 7 2 3 8 6 5
• 9. Sorting a Sequence With Merge Sort
• Sequence of length 1 is sorted
1 7 2 3 8 6 5
• 10. Sorting a Sequence With Merge Sort
• Sequence of length 1 is sorted
1 7 2 3 8 6 5
• 11. Sorting a Sequence With Merge Sort
• Merge these two into correct order
1 7 2 3 8 6 5
• 12. Sorting a Sequence With Merge Sort
• Return control to previous call.
• Now merge these two sub-sequences into correct order
1 7 2 8 3 6 5
• 13. Sorting a Sequence With Merge Sort
• The recursive call at line 7 is now completed
• First part of sequence is now fully sorted
1 7 2 8 6 5 3
• 14. Sorting a Sequence With Merge Sort
• Control now returns to the very first call of merge sort
• A recursive call is now made on the second part of the sequence
1 7 2 8 6 5 3
• 15. Sorting a Sequence With Merge Sort
• i =5, j =7, m =6
• Split sequence at item 6
1 7 2 8 6 5 3
• 16. Sorting a Sequence With Merge Sort
• Make a recursive call to sort these two items
• Split sequence at item 5
1 7 2 8 6 5 3
• 17. Sorting a Sequence With Merge Sort
• Make another recursive call on the first part of this subsequence
1 7 2 8 6 5 3
• 18. Sorting a Sequence With Merge Sort
• Sequence of length 1 is sorted
1 7 2 8 6 5 3
• 19. Sorting a Sequence With Merge Sort
• Make a recursive call on second part of sequence
• Sequence of length 1 is sorted
1 7 2 8 6 5 3
• 20. Sorting a Sequence With Merge Sort
• Now merge these 2 sub-sequences into correct order
1 7 2 8 6 5 3
• 21. Sorting a Sequence With Merge Sort
• Make a recursive call with the remaining element
1 7 2 8 6 5 3
• 22. Sorting a Sequence With Merge Sort
• Sequence of length 1 is sorted
1 7 2 8 6 5 3
• 23. Sorting a Sequence With Merge Sort
• Now merge these 2 sub-sequences into correct order
1 7 2 8 6 5 3
• 24. Sorting a Sequence With Merge Sort