Merge Sort

Consider a classroom with two groups of children lined up in order of height.

 

We want them to form one line, also arranged by height.

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

What's Happening Here?

Merge Sort

Start with a pile of data to be sorted

Repeatedly divide the pile in half until you get to singletons

Repeatedly merge the results.

Creative Commons Licence

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

2

5

1

7

split

split

split

merge

merge

merge

2

1

7

5

2

1

7

5

2

1

7

5

5

2

7

1

merge #1, 4 operations

merge #2, 4 operations

N= 4

2

1

7

5

2

1

7

5

2

1

7

5

split

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

split

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

merge

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

merge

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

merge

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

merge

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

merge

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

merge

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5

2

1

7

5


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


3
 


5
 


6
 


7
 


8
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


5
 


10
 


4
 


6
 


7
 


8
 


9
 


1
 


2
 


5
 


3
 


10
 


7
 


8
 


4
 


6
 


9
 


1
 


2
 


4
 


9
 


3
 


5
 


6
 


7
 


8
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


5
 


10
 


4
 


6
 


7
 


8
 


9
 


1
 


2
 


5
 


3
 


10
 


7
 


8
 


4
 


6
 


9
 


1
 


2
 


4
 


9
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 


1
 


2
 


3
 


4
 


5
 


6
 


7
 


8
 


9
 


10
 

Creative Commons Licence

STOP+THINK: How many split steps?

Creative Commons Licence

# merges depends on log2 N

O(N log2 N)

number of objects/cases/datapoints

time

"constant" time

"linear" time

"exponential" time

"quadratic" time

deck

By Dan Ryan