1 of 37

Lecture 11

More divide and conquer (median finding)

CSE 421 Autumn 2025

1

2 of 37

Previously…

2

3 of 37

Divide and conquer runtimes

  •  

The master theorem

3

 

4 of 37

Matrix multiplication

Matrix multiplication decomposes into smaller matrix multiplications!

4

Divide and conquer algorithm idea:

 

Runtime:

 

 

 

5 of 37

Strassen’s divide and conquer (1968)

  •  

5

Previously: we were directly multiplying, and then adding:

- Volker Strassen (89 years old!) about his algorithm

“It is really very simple. I was surprised that nobody had found it before.”

6 of 37

Strassen’s divide and conquer (1968)

6

Wikipedia article for Strassen’s algorithm

2

7 of 37

Today

7

More Divide and Conquer:

  • Integer multiplication
  • Median finding

8 of 37

Integer multiplication

  •  

8

 

Note: this is not in the word-RAM model. Instead, it’s counting the number of binary operations

9 of 37

Karatsuba’s algorithm

  •  

9

4 smaller multiplications

10 of 37

Karatsuba’s algorithm

  •  

10

4 smaller multiplications

11 of 37

Karatsuba’s algorithm

  •  

11

Can we do it with just 3 multiplications?

12 of 37

Karatsuba’s algorithm

  •  

12

Can we do it with just 3 multiplications?

Yes!

13 of 37

Karatsuba’s algorithm

  •  

13

Can we do it with just 3 multiplications?

Yes!

14 of 37

Karatsuba’s algorithm

  •  

14

Can we do it with just 3 multiplications?

Yes!

15 of 37

Improving integer multiplication

  •  

15

16 of 37

Multiplication

  •  

16

17 of 37

Median finding

  •  

17

 

 

Seems impossible…

… but we’ve seen other seemingly impossible things in this course

18 of 37

Median finding

18

Let’s try to design a divide and conquer algorithm.

 

Two natural possibilities:

 

 

Leaf-heavy computation

 

 

Most compute in largest conquer step

19 of 37

“Selection” finding

  •  

19

 

20 of 37

Selection finding

Find the 6th element

20

The idea:

Inspired by the Quicksort “pivot” approach:

  • We’ll pick a uniformly random element as a “pivot”.
  • Divide the array into two subset: elements smaller and larger than the pivot.
  • Figure out which subset the 6th element belongs to.
  • Recurse, i.e. solve Selection finding on the one subset that contains the 6th element!

 

The good news: only one branch!

But is our instance shrinking fast enough?

Depends on how lucky/unlucky we are with the pivot..

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

21 of 37

Selection finding with pivot (examples)

Find the 6th element

21

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

22 of 37

Selection finding with pivot (examples)

Find the 6th element

22

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

23 of 37

Selection finding with pivot (examples)

Find the 6th element

23

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

24 of 37

Selection finding with pivot (examples)

Find the 6th element

24

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

3

3

3

3

0

1

1

2

0

1

2

2

2

7

4

4

8

6

6

5

Where is the 6th element?

Randomly select a pivot

 

 

 

 

length 9

length 4

length 7

Recurse on this set

25 of 37

Selection finding with pivot (examples)

Find the 6th element

25

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

26 of 37

Selection finding with pivot (examples)

Find the 6th element

26

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

27 of 37

Selection finding with pivot (examples)

Find the 6th element

27

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

28 of 37

Selection finding with pivot (examples)

Find the 6th element

28

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

length 2

length 3

length 15

 

 

 

Where is the 6th element?

 

Recurse on this set

29 of 37

Selection finding with pivot (examples)

Find the 6th element

29

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

30 of 37

Selection finding with pivot (examples)

Find the 6th element

30

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

31 of 37

Selection finding with pivot (examples)

Find the 6th element

31

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

32 of 37

Selection finding with pivot (examples)

Find the 6th element

32

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

Randomly select a pivot

0

0

1

1

1

2

2

2

2

7

3

3

3

3

4

4

8

6

6

5

length 5

length 4

length 11

 

 

 

Where is the 6th element?

 

In fact, in this case, the 6th element is exactly the pivot! So we just output it right away.

33 of 37

Selection finding

  •  

33

 

34 of 37

Runtime analysis

  •  

34

35 of 37

Runtime analysis

  •  

35

36 of 37

Runtime analysis

  •  

36

37 of 37

Runtime analysis

  •  

37