Merge sort

1,579 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,579
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Merge sort

  1. 1. G R Lockwood, 2006 1 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sort the sequence 6, 5, 8, 3, 2, 7, 1Sort the sequence 6, 5, 8, 3, 2, 7, 1 using merge sortusing merge sort
  2. 2. G R Lockwood, 2006 2 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Split the sequenceSplit the sequence atat mm==((ii++jj)/2)/2  ii=1,=1, jj=7,=7, mm=4=4 66 55 88 33 22 77 11
  3. 3. G R Lockwood, 2006 3 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call to merge sortcall to merge sort  mm :=:= ((ii++jj)/2)/2== 2.52.5=2=2 66 55 88 33 22 77 11
  4. 4. G R Lockwood, 2006 4 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now sorting thisNow sorting this partpart  ii=1,=1, jj=2,=2, mm=1 and=1 and make a recursivemake a recursive callcall 66 55 88 33 22 77 11
  5. 5. G R Lockwood, 2006 5 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is already sorted,1 is already sorted, so returnso return 66 55 88 33 22 77 11
  6. 6. G R Lockwood, 2006 6 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 66 55 88 33 22 77 11
  7. 7. G R Lockwood, 2006 7 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Return control andReturn control and merge these 2 sub-merge these 2 sub- sequences intosequences into correct ordercorrect order 66 55 88 33 22 77 11
  8. 8. G R Lockwood, 2006 8 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now sort theNow sort the second part of thissecond part of this subsequencesubsequence 55 66 88 33 22 77 11
  9. 9. G R Lockwood, 2006 9 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 55 66 88 33 22 77 11
  10. 10. G R Lockwood, 2006 10 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 55 66 88 33 22 77 11
  11. 11. G R Lockwood, 2006 11 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Merge these twoMerge these two into correct orderinto correct order 55 66 88 33 22 77 11
  12. 12. G R Lockwood, 2006 12 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Return control toReturn control to previous call.previous call.  Now merge theseNow merge these two sub-sequencestwo sub-sequences into correct orderinto correct order 55 66 33 88 22 77 11
  13. 13. G R Lockwood, 2006 13 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  The recursive callThe recursive call at line 7 is nowat line 7 is now completedcompleted  First part ofFirst part of sequence is nowsequence is now fully sortedfully sorted 33 55 66 88 22 77 11
  14. 14. G R Lockwood, 2006 14 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Control nowControl now returns to the veryreturns to the very first call of mergefirst call of merge sortsort  A recursive call isA recursive call is now made on thenow made on the second part of thesecond part of the sequencesequence 33 55 66 88 22 77 11
  15. 15. G R Lockwood, 2006 15 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  ii=5,=5, jj=7,=7, mm=6=6  Split sequence atSplit sequence at item 6item 6 33 55 66 88 22 77 11
  16. 16. G R Lockwood, 2006 16 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call to sort thesecall to sort these two itemstwo items  Split sequence atSplit sequence at item 5item 5 33 55 66 88 22 77 11
  17. 17. G R Lockwood, 2006 17 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make anotherMake another recursive call onrecursive call on the first part of thisthe first part of this subsequencesubsequence 33 55 66 88 22 77 11
  18. 18. G R Lockwood, 2006 18 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 33 55 66 88 22 77 11
  19. 19. G R Lockwood, 2006 19 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call on second partcall on second part of sequenceof sequence  Sequence of lengthSequence of length 1 is sorted1 is sorted 33 55 66 88 22 77 11
  20. 20. G R Lockwood, 2006 20 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now merge these 2Now merge these 2 sub-sequencessub-sequences into correct orderinto correct order 33 55 66 88 22 77 11
  21. 21. G R Lockwood, 2006 21 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Make a recursiveMake a recursive call with thecall with the remaining elementremaining element 33 55 66 88 22 77 11
  22. 22. G R Lockwood, 2006 22 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Sequence of lengthSequence of length 1 is sorted1 is sorted 33 55 66 88 22 77 11
  23. 23. G R Lockwood, 2006 23 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Now merge these 2Now merge these 2 sub-sequences intosub-sequences into correct ordercorrect order 33 55 66 88 22 77 11
  24. 24. G R Lockwood, 2006 24 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Return to the firstReturn to the first callcall  Now merge these 2Now merge these 2 sub-sequences intosub-sequences into correct ordercorrect order 33 55 66 88 11 22 77
  25. 25. G R Lockwood, 2006 25 Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort  Merge sort isMerge sort is completedcompleted 11 22 33 55 66 77 88

×