Download - Merge sort b

Transcript
Page 1: Merge sort b

Merge sort

Page 2: Merge sort b

Let’s start with an unsorted list

Page 3: Merge sort b

Divide the list into two lists

Page 4: Merge sort b

Divide that list up again

Page 5: Merge sort b

Divide that list up again

Page 6: Merge sort b

And again...

Page 7: Merge sort b

A single item list must be sorted✓

Page 8: Merge sort b

go to the next list, a single item list must be sorted✓

Page 9: Merge sort b

jjii

KKNow take that pair and merge

Page 10: Merge sort b

jjii

KK

Look down both lists taking the smallest

Page 11: Merge sort b

jjii

✓KK

When both lists run out you have a sorted list

Page 12: Merge sort b

jjii Now this pair is sorted

Page 13: Merge sort b

✓Do the same with the next pair

Page 14: Merge sort b

✓Each is a list of one item (so sorted)

Page 15: Merge sort b

jjii

KK We merge again

Page 16: Merge sort b

jjii

KK

Creating a new sorted list

Page 17: Merge sort b

Now we have two lists of pairs...

Page 18: Merge sort b

So we merge them

Page 19: Merge sort b

jjii

KK

Look down both lists taking the smallest

Page 20: Merge sort b

jjii

KK

Page 21: Merge sort b

jjii

KK

Look down both lists taking the smallest

Page 22: Merge sort b

jjii

KK

Look down both lists taking the smallest

Page 23: Merge sort b

jjii

KK

Look down both lists taking the smallest

Page 24: Merge sort b

jjii KK

Look down both lists taking the smallest

Page 25: Merge sort b

the list is sorted and you can begin the next quarter

Page 26: Merge sort b

A single item list must be sorted✓

Page 27: Merge sort b

A single item list must be sorted✓

Page 28: Merge sort b

jjii KK Merge this pair

Page 29: Merge sort b

jjii

KK

Pick the smaller item

Page 30: Merge sort b

jjii KKWhen both lists run out you have a sorted list

Page 31: Merge sort b
Page 32: Merge sort b

Sort the next pair

Page 33: Merge sort b

A single item list must be sorted

Page 34: Merge sort b

A single item list must be sorted

Page 35: Merge sort b

Merge the single item lists

Page 36: Merge sort b

jjii KK Merge this pair

Page 37: Merge sort b

jjii KK

Pick the smaller item

Page 38: Merge sort b

jjii KKPick the smaller item

Page 39: Merge sort b

Now we have two lists of pairs...

Page 40: Merge sort b

ii

jj KKSo we merge them

Page 41: Merge sort b

ii

jj KK

Pick the smaller item

Page 42: Merge sort b

ii jj

KKPick the smaller item

Page 43: Merge sort b

ii jj KK

Pick the smaller item

Page 44: Merge sort b

ii jj

KK

Pick the smaller item

Page 45: Merge sort b

the pairs lists are now merged

Page 46: Merge sort b

ii jj KK

Now we merge the quarters

Page 47: Merge sort b

ii

jj KK

Pick the smaller item

Page 48: Merge sort b

ii jj KK

and move on

Page 49: Merge sort b

ii jj KK

Pick the smaller item

Page 50: Merge sort b

ii jj KK

and move on

Page 51: Merge sort b

ii jj KK

Pick the smaller item

Page 52: Merge sort b

ii jj KK

and move on

Page 53: Merge sort b

ii jj KK

Pick the smaller item

Page 54: Merge sort b

ii jj KK

and move on

Page 55: Merge sort b

The first half is now sorted

Page 56: Merge sort b

Quickly we do the other half

Page 57: Merge sort b

Divide...

Page 58: Merge sort b

and Divide...

Page 59: Merge sort b

and Divide until the lists can be no smaller

Page 60: Merge sort b

Merge pairs...

Page 61: Merge sort b

Merge quaters

Page 62: Merge sort b

Finally the second half is sorted

Page 63: Merge sort b

A final merge of both halves makes the list sorted

Page 64: Merge sort b

ii jj KK

Merging lists takes as long as the list (order(N))

You do this log(N) timesMaking algorithm Order (N log(N))

Page 65: Merge sort b

ii jj KK

Merging lists takes as long as the list (order(N))

You do this log(N) timesMaking algorithm Order (N log(N))

Merging lists takes as long as the list (order(N))

You do this log(N) timesMaking algorithm Order (N log(N))

Pity you need extra space to ‘merge’ into otherwise it would be pretty cool