Sorting a Sequence With Merge Sort u Sort

  • Slides: 25
Download presentation
Sorting a Sequence With Merge Sort u Sort the sequence 6, 5, 8, 3,

Sorting a Sequence With Merge Sort u Sort the sequence 6, 5, 8, 3, 2, 7, 1 using merge sort G R Lockwood, 2006 1

Sorting a Sequence With Merge Sort u u Split the sequence at m= (i+j)/2

Sorting a Sequence With Merge Sort u u Split the sequence at m= (i+j)/2 i=1, j=7, m=4 6 5 8 3 2 7 1 G R Lockwood, 2006 2

Sorting a Sequence With Merge Sort u u Make a recursive call to merge

Sorting a Sequence With Merge Sort u u Make a recursive call to merge sort m : = (i+j)/2 = 2. 5 =2 6 5 8 3 2 7 1 G R Lockwood, 2006 3

Sorting a Sequence With Merge Sort u u Now sorting this part i=1, j=2,

Sorting a Sequence With Merge Sort u u Now sorting this part i=1, j=2, m=1 and make a recursive call 6 5 8 3 2 7 1 G R Lockwood, 2006 4

Sorting a Sequence With Merge Sort u Sequence of length 1 is already sorted,

Sorting a Sequence With Merge Sort u Sequence of length 1 is already sorted, so return 6 5 8 3 2 7 1 G R Lockwood, 2006 5

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 6

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 6 5 8 3 2 7 1 G R Lockwood, 2006 6

Sorting a Sequence With Merge Sort u Return control and merge these 2 subsequences

Sorting a Sequence With Merge Sort u Return control and merge these 2 subsequences into correct order 6 5 8 3 2 7 1 G R Lockwood, 2006 7

Sorting a Sequence With Merge Sort u Now sort the second part of this

Sorting a Sequence With Merge Sort u Now sort the second part of this subsequence 5 6 8 3 2 7 1 G R Lockwood, 2006 8

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 5

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 5 6 8 3 2 7 1 G R Lockwood, 2006 9

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 5

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 5 6 8 3 2 7 1 G R Lockwood, 2006 10

Sorting a Sequence With Merge Sort u Merge these two into correct order 5

Sorting a Sequence With Merge Sort u Merge these two into correct order 5 6 8 3 2 7 1 G R Lockwood, 2006 11

Sorting a Sequence With Merge Sort u u Return control to previous call. Now

Sorting a Sequence With Merge Sort u u Return control to previous call. Now merge these two sub-sequences into correct order 5 6 3 8 2 7 1 G R Lockwood, 2006 12

Sorting a Sequence With Merge Sort u u The recursive call at line 7

Sorting a Sequence With Merge Sort u u The recursive call at line 7 is now completed First part of sequence is now fully sorted 3 5 6 8 2 7 1 G R Lockwood, 2006 13

Sorting a Sequence With Merge Sort u u Control now returns to the very

Sorting a Sequence With Merge Sort u u Control now returns to the very first call of merge sort A recursive call is now made on the second part of the sequence 3 5 6 8 2 7 1 G R Lockwood, 2006 14

Sorting a Sequence With Merge Sort u u i=5, j=7, m=6 Split sequence at

Sorting a Sequence With Merge Sort u u i=5, j=7, m=6 Split sequence at item 6 3 5 6 8 2 7 1 G R Lockwood, 2006 15

Sorting a Sequence With Merge Sort u u Make a recursive call to sort

Sorting a Sequence With Merge Sort u u Make a recursive call to sort these two items Split sequence at item 5 3 5 6 8 2 7 1 G R Lockwood, 2006 16

Sorting a Sequence With Merge Sort u Make another recursive call on the first

Sorting a Sequence With Merge Sort u Make another recursive call on the first part of this subsequence 3 5 6 8 2 7 1 G R Lockwood, 2006 17

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 3

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 3 5 6 8 2 7 1 G R Lockwood, 2006 18

Sorting a Sequence With Merge Sort u u Make a recursive call on second

Sorting a Sequence With Merge Sort u u Make a recursive call on second part of sequence Sequence of length 1 is sorted 3 5 6 8 2 7 1 G R Lockwood, 2006 19

Sorting a Sequence With Merge Sort u Now merge these 2 sub-sequences into correct

Sorting a Sequence With Merge Sort u Now merge these 2 sub-sequences into correct order 3 5 6 8 2 7 1 G R Lockwood, 2006 20

Sorting a Sequence With Merge Sort u Make a recursive call with the remaining

Sorting a Sequence With Merge Sort u Make a recursive call with the remaining element 3 5 6 8 2 7 1 G R Lockwood, 2006 21

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 3

Sorting a Sequence With Merge Sort u Sequence of length 1 is sorted 3 5 6 8 2 7 1 G R Lockwood, 2006 22

Sorting a Sequence With Merge Sort u Now merge these 2 sub-sequences into correct

Sorting a Sequence With Merge Sort u Now merge these 2 sub-sequences into correct order 3 5 6 8 2 7 1 G R Lockwood, 2006 23

Sorting a Sequence With Merge Sort u u Return to the first call Now

Sorting a Sequence With Merge Sort u u Return to the first call Now merge these 2 sub-sequences into correct order 3 5 6 8 1 2 7 G R Lockwood, 2006 24

Sorting a Sequence With Merge Sort u Merge sort is completed 1 2 3

Sorting a Sequence With Merge Sort u Merge sort is completed 1 2 3 5 6 7 8 G R Lockwood, 2006 25