merge sort

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

Upload: sumedha

Post on 02-Nov-2014

27 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 11

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sort the sequence 6, 5, 8, 3, 2, 7, 1 Sort the sequence 6, 5, 8, 3, 2, 7, 1 using merge sortusing merge sort

Page 2: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 22

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Split the sequence Split the sequence at at mm==((ii++jj)/2)/2

ii=1, =1, jj=7, =7, mm=4=4

66

55

88

33

22

77

11

Page 3: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 33

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Make a recursive Make 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

Page 4: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 44

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Now sorting this Now sorting this partpart

ii=1, =1, jj=2, =2, mm=1 and =1 and make a recursive make a recursive callcall

66

55

88

33

22

77

11

Page 5: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 55

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sequence of length Sequence of length 1 is already sorted, 1 is already sorted, so returnso return

66

55

88

33

22

77

11

Page 6: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 66

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sequence of length Sequence of length 1 is sorted1 is sorted

66

55

88

33

22

77

11

Page 7: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 77

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Return control and Return control and merge these 2 sub-merge these 2 sub-sequences into sequences into correct ordercorrect order

66

55

88

33

22

77

11

Page 8: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 88

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Now sort the Now sort the second part of this second part of this subsequencesubsequence

55

66

88

33

22

77

11

Page 9: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 99

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sequence of length Sequence of length 1 is sorted1 is sorted

55

66

88

33

22

77

11

Page 10: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1010

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sequence of length Sequence of length 1 is sorted1 is sorted

55

66

88

33

22

77

11

Page 11: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1111

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Merge these two Merge these two into correct orderinto correct order

55

66

88

33

22

77

11

Page 12: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1212

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Return control to Return control to previous call.previous call.

Now merge these Now merge these two sub-sequences two sub-sequences into correct orderinto correct order

55

66

33

88

22

77

11

Page 13: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1313

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

The recursive call The recursive call at line 7 is now at line 7 is now completedcompleted

First part of First part of sequence is now sequence is now fully sortedfully sorted

33

55

66

88

22

77

11

Page 14: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1414

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Control now Control now returns to the very returns to the very first call of merge first call of merge sortsort

A recursive call is A recursive call is now made on the now made on the second part of the second part of the sequencesequence

33

55

66

88

22

77

11

Page 15: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1515

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

ii=5, =5, jj=7, =7, mm=6=6 Split sequence at Split sequence at

item 6item 6

33

55

66

88

22

77

11

Page 16: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1616

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Make a recursive Make a recursive call to sort these call to sort these two itemstwo items

Split sequence at Split sequence at item 5item 5

33

55

66

88

22

77

11

Page 17: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1717

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Make another Make another recursive call on recursive call on the first part of this the first part of this subsequencesubsequence

33

55

66

88

22

77

11

Page 18: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1818

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sequence of length Sequence of length 1 is sorted1 is sorted

33

55

66

88

22

77

11

Page 19: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 1919

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Make a recursive Make a recursive call on second part call on second part of sequenceof sequence

Sequence of length Sequence of length 1 is sorted1 is sorted

33

55

66

88

22

77

11

Page 20: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 2020

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Now merge these 2 Now merge these 2 sub-sequences sub-sequences into correct orderinto correct order

33

55

66

88

22

77

11

Page 21: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 2121

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Make a recursive Make a recursive call with the call with the remaining elementremaining element

33

55

66

88

22

77

11

Page 22: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 2222

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Sequence of length Sequence of length 1 is sorted1 is sorted

33

55

66

88

22

77

11

Page 23: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 2323

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Now merge these 2 Now merge these 2 sub-sequences into sub-sequences into correct ordercorrect order

33

55

66

88

22

77

11

Page 24: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 2424

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Return to the first Return to the first callcall

Now merge these 2 Now merge these 2 sub-sequences into sub-sequences into correct ordercorrect order

33

55

66

88

11

22

77

Page 25: Merge sort

G R Lockwood, 2006G R Lockwood, 2006 2525

Sorting a Sequence With Merge SortSorting a Sequence With Merge Sort

Merge sort is Merge sort is completedcompleted

11

22

33

55

66

77

88