FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer...

22
FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean

Transcript of FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer...

Page 1: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

FASTER SORT using

RECURSION :

MERGE SORT

2015-T2 Lecture 15

School of Engineering and Computer Science, Victoria University of Wellington

CO

MP 1

03

Marcus Frean

Page 2: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

RECAP-TODAYRECAP Recursion

TODAY Fast Sorts: Divide and Conquer Merge Sort (and analysis)

Announcements Assignment #5: came out last Friday, but is due

Friday 11th Sept (ie. you get a really loooong time to complete it)

Mid-term test: is this Friday

2

Page 3: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

3

the test this Friday 20% of final grade, and takes 45 mins

Rooms FROM TO Maclaurin 101 A J Maclaurin 102 K M Hunter 323 N Z

Please be on time, and bring your ID card. “Not-quite-Exam conditions”:

keep a vacant seat between each put your ID card where it can be seen – at the end of the row. (no need to leave bags at the front)

What’s in? everything up to and including Recursion old tests are a guide for what to expect

Page 4: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

4Stresses and strains

1st year is stressful to most people. How’s your “mental health” and how well connected are you socially? Do any of the following resonate?

freaked out, wound up flat, exhausted lonely, vulnerable just all over the place

it’s normal in “doses”, but if this is you much of the time, that’s not ideal.

Talking to a trained person (student health) is great, but there’s a long wait.

If you’re struggling academically (e.g. you might not make 7 assignments, or you do badly on tomorrow’s test), then :

[email protected] on Craig’s door: CO 253 (Cotton building)

4

Page 5: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

5Well-being: some homework

connect talk and listen, be there, feel connected

be active do what you can do, enjoy what you do, move your

mood

take notice re-remembering the simple things that give you joy

: good but the opposite (ruminating) : sucketh

5

And these are No-brainers for keeping a resilient brain: sleep intake under control (caffeine, booze, sugar) exercise

for fun:waitbutwhy.com

especially “Taming the mammoth”, and “The

procrastination matrix”

Page 6: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

6

Insertion sort, Selection Sort, Bubble Sort: All slow (except Insertion sort on almost sorted lists) O(n2 )

Problem: Insertion and Bubble

only compare adjacent items only move items one step at a time

Selection compares every pair of items ignores results of previous comparisons.

Solution: Must compare and swap items at a distance Must not perform redundant comparisons

Slow Sorts

Page 7: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

7

Divide and Conquer SortsTo Sort:

Split Sort each part

(recursive) Combine

Where does the

work happen?

MergeSort: split is trivial combine does all the work

QuickSort: split does all the work combine is trivial

Array

Sorted Array

Split

Combine

SubArray SubArray

SortedSubArray SortedSubArray

Sort Sort

Split

Combine

SubArray SubArray

Sort Sort

SortedSubArraySortedSubArray

Split

Combine

SubArray SubArray

Sort Sort

SortedSubArraySortedSubArray

Page 8: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

8

MergeSort : the concept

Page 9: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
Page 10: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

10

MergeSort : the order when recursing...

Page 11: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

11

6 7 8 9 10 110 1 2 3 4 5

MergeSort: the ‘merge’ method

6 7 8 9 10 110 1 2 3 4 5

“from” array

“to”

array

Page 12: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

12

Merge/** Merge from[low..mid-1] with from[mid..high-1] into

to[low..high-1.*/private static <E> void merge(E[] from, E[] to, int low, int mid, int high, Comparator<E> comp){

int index = low;        // where we will put the item into "to“int indxLeft = low;    // index into the lower half of the "from" rangeint indxRight = mid;   // index into the upper half of the "from" rangewhile (indxLeft<mid && indxRight < high){

if (comp.compare(from[indxLeft], from[indxRight]) <=0)to[index++] = from[indxLeft++];

elseto[index++] = from[indxRight++];

}// copy over the remainder. Note only one loop will do anything.while (indxLeft<mid)

to[index++] = from[indxLeft++];while (indxRight<high)

to[index++] = from[indxRight++];}

Page 13: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

13

MergeSort – a wrapper method that starts it

It looks like we an extra temporary array for each “level” (how many levels are there?)

Somewhat REMARKABLY, it turns out we only need one (extra), and at each layer we can treat the other array as “storage”

We start with a wrapper makes this second array, and fills it with a copy of the original data.

public static <E> void mergeSort(E[] data, int size, Comparator<E> comp){E[] other = (E[])new Object[size];for (int i=0; i<size; i++)  

temp[i]=data[i];mergeSort(data, temp, 0, size, comp);

}

Page 14: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

14

MergeSort – the recursive method

private static <E> void mergeSort(E[] data, E[] other, int low, int high, Comparator<E> comp){

// sort items from low..high-1, using the other arrayif (high > low+1){

int mid = (low+high)/2;// mid = low of upper 1/2, = high of lower half.mergeSort(other, data, low, mid, comp);mergeSort(other, data, mid, high, comp);

merge(other, data, low, mid, high, comp);}

}

there are multiple calls to the recursive method in here.

this will make a "tree" structure we swap other and data at each recursive call (=

each “level”)

Page 15: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

15

MergeSort: recursion

ms(d, t, 0,16)

ms(t, d, 0,8) ms(t, d, 8,16) mg(t, d, 0,8,16)

ms(d,t,0,4) ms(d,t,4,8) mg(d,t,0,4,8)

ms(t,d,0,2)ms(t,d,2,4)

mg(t,d,0,2,4)

ms(d,t,0,1)

ms(d,t,1,2)

mg(d,t,0,1,2)

ms(d,t,2,3)

ms(d,t,3,4)

mg(d,t,2,3,4)

ms(t,d,4,6) ms(t,d,6,8) mg(t,d,4,6,8)

ms(d,t,4,5)

ms(d,t,5,6)

mg(d,t,4,5,6)

ms(d,t,6,7)

ms(d,t,7,8)

mg(d,t,6,7,8)

ms(d,t,8,12) ms(d,t,12,16) mg(d,t,0,8,16)

(...) (...)

Page 16: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

16

MergeSort – will the two arrays 'mess up'?data

to sort

other

to sort

'working'

'working'

merge

merge

'working'

merge

to sort

Page 17: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

17

data [p a1 r f e q2 w q1 t z2 x c v b z1 a2 ]

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

msort(0..16) [p a1 r f e q2 w q1 t z2 x c v b z1 a2 ]

msort(0..8) [p a1 r f e q2 w q1 ]msort(0..4 ) [p a1 r

f ]msort(0..2 ) [p

a1 ]msort(0..1 )

[p ]msort(1..2) [ a1 ]merge(0.1.2) [a1

p ]msort(2..4) [ r f ]msort(2..3) [ r ]msort(3..4) [ f ]merge(2.3.4) [ f

r ]merge(0.2.4) [a1 f p

r ]msort(4..8) [ e q2 w q1 ]msort(4..6) [ e q2 ] : :merge(4.5.6) [ e

q2 ]msort(6..8) [ w q1 ] : :merge(6.7.8) [ q1

w ]merge(4.6.8) [ e q2 q1

w ]merge(0.4.8) [a1 e f p q2 q1 r

w ]msort(8..16) [ t z2 x c v b z1

a2 ] : :merge(8.12.16) [ a2 b c t v x z1

z2 ]merge(0.8.16) [a1 a2 b c e f p q2 q1 r t v w x z1

z2 ]

Page 18: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

18

Sorting Algorithm costs: Insertion sort, Selection Sort:

All slow (except Insertion sort on almost-sorted lists)

O(n 2)

Merge Sort?

There’s no inner loop! How do you analyse recursive algorithms?

Page 19: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

19

MergeSortprivate static <E> void mergeSort (E[ ] data, E[ ] other, int low, int high,

Comparator<E> comp) {

  if (high > low+1) {      int mid = (low+high)/2;         mergeSort(other, data, low, mid, comp);      mergeSort(other, data, mid, high, comp);     merge(other, data, low, mid, high, comp);}

}

Cost of mergeSort: Three steps:

first recursive call second recursive call merge: has to copy over (high-

low) items

Page 20: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

20

MergeSort Cost Level 1: 2 * n/2 = n Level 2: 4 * n/4 = n Level 3: 8 * n/8 = n Level 4: 16 * n/16= n

so in general, at any level k, there are n comparisons

How many levels? the number of times you can halve n is log n

Total cost? = O( )

n = 1,000 n = 1,000,000

Page 21: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

21

Analysing with Recurrence Relations

private static <E> void mergeSort(E[] data, E[] other, int low, int high, Comparator<E> comp){

  if (high > low+1){      int mid = (low+high)/2;         mergeSort(other, data, low, mid, comp);      mergeSort(other, data, mid, high, comp);     merge(other, data, low, mid, high, comp);}

} Cost of mergeSort = C(n)

C(n) = C(n/2) + C(n/2) + n = 2 C(n/2) + n

Recurrence Relation: (we will) Solve by repeated substitution & find pattern (we could) Solve by general method

Page 22: FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

22

Solving Recurrence RelationsC(n) = 2 C(n/2) + n

= 2 [ 2 C(n/4) + n/2] + n

= 4 C(n/4) + 2 n

= 4 [ 2 (C(n/8) + n/4] + 2 n

= 8 C(n/8) + 3 n

= 16 C(n/16) + 4 n

:

= 2k C( n/2k ) + k * nwhen n = 2k, k = log(n)

= n C (1) + log(n) * n

and since C(1) = 0,

C(n) = log(n) * n http://www.youtube.com/watch?v=XaqR3G_NVoo