+ All Categories
Home > Documents > New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff():...

New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff():...

Date post: 26-Sep-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
140
Dennis Komm Programming and Problem-Solving Recursive Algorithms Spring 2020 – April 23, 2020
Transcript
Page 1: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Dennis Komm

Programming and Problem-SolvingRecursive Algorithms

Spring 2020 – April 23, 2020

Page 2: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

Page 3: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

This analogy is not entirely correctSuch functions are called recursive functions

Not from Merriam-Webster dictionary

re·frig·er·a·torA refrigerator

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 1 / 27

Page 4: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

This analogy is not entirely correctSuch functions are called recursive functions

Not from Merriam-Webster dictionary

re·frig·er·a·torA refrigerator

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 1 / 27

Page 5: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

This analogy is not entirely correctSuch functions are called recursive functions

Not from Merriam-Webster dictionary

re·frig·er·a·torA refrigerator

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 1 / 27

Page 6: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

This analogy is not entirely correctSuch functions are called recursive functions

Not from Merriam-Webster dictionary

re·frig·er·a·torA refrigerator

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 1 / 27

Page 7: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 8: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 9: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 10: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

f()print(”Hello world!”)f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 11: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

f()print(”Hello world!”)f()

print(”Hello world!”)f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 12: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

f()print(”Hello world!”)f()

print(”Hello world!”)f()

print(”Hello world!”)f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 13: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

f()print(”Hello world!”)f()

print(”Hello world!”)f()

print(”Hello world!”)f()

print(”Hello world!”)f()

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 14: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

This results in an endless loop

def f():print("Hello world!")f()

f()print(”Hello world!”)f()

print(”Hello world!”)f()

print(”Hello world!”)f()

print(”Hello world!”)f() ∞

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 2 / 27

Page 15: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 16: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 17: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

Parameter (or any local variable) isnewly created for every function call

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 18: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

f(4)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 19: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

f(4)

print(4)if 4 == 1:

returnelse:

f(3)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 20: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

f(4)

print(4)if 4 == 1:

returnelse:

f(3)

print(3)if 3 == 1:

returnelse:

f(2)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 21: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

f(4)

print(4)if 4 == 1:

returnelse:

f(3)

print(3)if 3 == 1:

returnelse:

f(2)

print(2)if 2 == 1:

returnelse:

f(1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 22: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

f(4)

print(4)if 4 == 1:

returnelse:

f(3)

print(3)if 3 == 1:

returnelse:

f(2)

print(2)if 2 == 1:

returnelse:

f(1)

print(1)if 1 == 1:

returnelse:

f(0)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 23: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Functions

We use parameters to end after a finite number of calls

def f(k):print(k)if k == 1:

returnelse:

f(k-1)

f(4)

print(4)if 4 == 1:

returnelse:

f(3)

print(3)if 3 == 1:

returnelse:

f(2)

print(2)if 2 == 1:

returnelse:

f(1)

print(1)if 1 == 1:

returnelse:

f(0)

×(Termination)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 3 / 27

Page 24: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Factorial and Sum

Page 25: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively

Factorial of a natural number n is defined by

fact(n) = n! = n · (n− 1) · (n− 2) · · · · · 2 · 1

For instance, 7! = 7 · 6 · 5 · 4 · 3 · 2 · 1 = 5040We observe

n! = n · (n− 1)! = n · (n− 1) · (n− 2)! = . . .

Function can be computed recursively by

1! = 1 and fact(n) = n · fact(n− 1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 4 / 27

Page 26: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively

Factorial of a natural number n is defined by

fact(n) = n! = n · (n− 1) · (n− 2) · · · · · 2 · 1

For instance, 7! = 7 · 6 · 5 · 4 · 3 · 2 · 1 = 5040

We observe

n! = n · (n− 1)! = n · (n− 1) · (n− 2)! = . . .

Function can be computed recursively by

1! = 1 and fact(n) = n · fact(n− 1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 4 / 27

Page 27: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively

Factorial of a natural number n is defined by

fact(n) = n! = n · (n− 1) · (n− 2) · · · · · 2 · 1

For instance, 7! = 7 · 6 · 5 · 4 · 3 · 2 · 1 = 5040We observe

n! = n · (n− 1)! = n · (n− 1) · (n− 2)! = . . .

Function can be computed recursively by

1! = 1 and fact(n) = n · fact(n− 1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 4 / 27

Page 28: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively

Factorial of a natural number n is defined by

fact(n) = n! = n · (n− 1) · (n− 2) · · · · · 2 · 1

For instance, 7! = 7 · 6 · 5 · 4 · 3 · 2 · 1 = 5040We observe

n! = n · (n− 1)! = n · (n− 1) · (n− 2)! = . . .

Function can be computed recursively by

1! = 1 and fact(n) = n · fact(n− 1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 4 / 27

Page 29: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 30: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

As before, parameter is newlycreated for every function call

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 31: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Last call returnsfixed value 1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 32: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)Other calls returnn times “factorial of n-1”

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 33: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 34: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 35: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 36: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 37: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 38: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3)

fact(2)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 39: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3)

fact(2)

fact(1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 40: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3)

fact(2)

fact(1) return 1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 41: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3)

fact(2) return 2 * 1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 42: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3)

fact(2) return 2

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 43: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3) return 3 * 2

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 44: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4)

fact(3) return 6

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 45: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4) return 4 * 6

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 46: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5)

fact(4) return 24

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 47: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5) return 5 * 24

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 48: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6)

fact(5) return 120

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 49: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6) return 6 * 120

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 50: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7)

fact(6) return 720

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 51: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7) return 7 * 720

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 52: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the Factorial Recursively – in Python

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

Call Stack

fact(7) return 5040

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 5 / 27

Page 53: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Exercise – Computing a Sum Recursively

Implement a recursivefunction that

takes a parameter n

and returns the sum of the firstn natural numbers

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 6 / 27

Page 54: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Exercise – Computing a Sum Recursively

Both recursive functions can be implemented with the same idea

def fact(n):if n == 1:

return 1else:

return n * fact(n-1)

def thesum(n):if n == 1:

return 1else:

return n + thesum(n-1)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 7 / 27

Page 55: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

There are alternatives using loops

def fact(n):i = 1result = 1while i < n:

i += 1result *= i

return result

def thesum(n):i = 1result = 1while i < n:

i += 1result += i

return result

For the sum, there is also a closed form (from the Bubblesort analysis)

def thesum(n):return n * (n+1) / 2

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 8 / 27

Page 56: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

There are alternatives using loops

def fact(n):i = 1result = 1while i < n:

i += 1result *= i

return result

def thesum(n):i = 1result = 1while i < n:

i += 1result += i

return result

For the sum, there is also a closed form (from the Bubblesort analysis)

def thesum(n):return n * (n+1) / 2

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 8 / 27

Page 57: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

If repeated statements are implemented using loops, we speak ofiterative programming

For all problems, there exist both iterative and recursive solutions

The recursive solution can often be viewed as more “elegant”

The implementation using recursion is often shorter (more concise) to write

. . . but almost never faster to execute

What should be used, depends on multiple factors

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 9 / 27

Page 58: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

If repeated statements are implemented using loops, we speak ofiterative programming

For all problems, there exist both iterative and recursive solutions

The recursive solution can often be viewed as more “elegant”

The implementation using recursion is often shorter (more concise) to write

. . . but almost never faster to execute

What should be used, depends on multiple factors

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 9 / 27

Page 59: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

If repeated statements are implemented using loops, we speak ofiterative programming

For all problems, there exist both iterative and recursive solutions

The recursive solution can often be viewed as more “elegant”

The implementation using recursion is often shorter (more concise) to write

. . . but almost never faster to execute

What should be used, depends on multiple factors

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 9 / 27

Page 60: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

If repeated statements are implemented using loops, we speak ofiterative programming

For all problems, there exist both iterative and recursive solutions

The recursive solution can often be viewed as more “elegant”

The implementation using recursion is often shorter (more concise) to write

. . . but almost never faster to execute

What should be used, depends on multiple factors

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 9 / 27

Page 61: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursion vs. Iteration

If repeated statements are implemented using loops, we speak ofiterative programming

For all problems, there exist both iterative and recursive solutions

The recursive solution can often be viewed as more “elegant”

The implementation using recursion is often shorter (more concise) to write

. . . but almost never faster to execute

What should be used, depends on multiple factors

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 9 / 27

Page 62: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Euclid’s Algorithm Recursively

Page 63: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Euclid’s Algorithm RecursivelyEuclid’s Algorithmknown from the first lecture

Input: integers a > 0, b > 0Output: gcd of a and b

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return a

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 10 / 27

Page 64: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Euclid’s Algorithm RecursivelyEuclid’s Algorithmknown from the first lecture

Input: integers a > 0, b > 0Output: gcd of a and b

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return aa b

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 10 / 27

Page 65: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Euclid’s Algorithm RecursivelyEuclid’s Algorithmknown from the first lecture

Input: integers a > 0, b > 0Output: gcd of a and b

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return aa b a b

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 10 / 27

Page 66: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Euclid’s Algorithm RecursivelyEuclid’s Algorithmknown from the first lecture

Input: integers a > 0, b > 0Output: gcd of a and b

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return aa b a b a b

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 10 / 27

Page 67: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Euclid’s Algorithm RecursivelyEuclid’s Algorithmknown from the first lecture

Input: integers a > 0, b > 0Output: gcd of a and b

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return aa b a b a b a b

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 10 / 27

Page 68: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Exercise – Computing the GCD Recursively

Implement Euclid’s Algorithm

as a recursive Python function

that takes two parameters a and b

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return a

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 11 / 27

Page 69: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return a

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 12 / 27

Page 70: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

def euclid(a, b):while b != 0:

if a > b:a = a - b

else:b = b - a

return a

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 12 / 27

Page 71: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 72: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 73: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 74: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 75: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

euclid(34, 17)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 76: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

euclid(34, 17)

euclid(17, 17)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 77: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

euclid(34, 17)

euclid(17, 17)

euclid(17, 0)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 78: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

euclid(34, 17)

euclid(17, 17)

euclid(17, 0) return 17

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 79: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

euclid(34, 17)

euclid(17, 17) return 17

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 80: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17)

euclid(34, 17) return 17

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 81: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68)

euclid(51, 17) return 17

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 82: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68)

euclid(51, 68) return 17

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 83: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Computing the GCD Recursively

def euclid(a, b):if b == 0:

return aelse:

if a > b:return euclid(a - b, b)

else:return euclid(a, b - a)

Call Stackreturn value is passed through

euclid(119, 68) return 17

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 13 / 27

Page 84: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Sorting and SearchingBinary Search

Page 85: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Binary Search

def binsearch(data, searched):left = 0right = len(data) - 1while left <= right:

current = (left + right) // 2if data[current] == searched:

return currentelif data[current] > searched:

right = current - 1else:

left = current + 1return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 14 / 27

Page 86: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Recursive Implementation

Function again takes parameters data and for the given list and thesearched element

Two parameters left and right define the current search space

In a single call, left and right are not changed

ï No loop

current is again computed as (left + right) // 2

Again consider position data[current]

If searched is not found, call the function recursively and either adjust leftor right accordingly

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 15 / 27

Page 87: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Recursive Implementation

Function again takes parameters data and for the given list and thesearched element

Two parameters left and right define the current search space

In a single call, left and right are not changed

ï No loop

current is again computed as (left + right) // 2

Again consider position data[current]

If searched is not found, call the function recursively and either adjust leftor right accordingly

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 15 / 27

Page 88: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Recursive Implementation

Function again takes parameters data and for the given list and thesearched element

Two parameters left and right define the current search space

In a single call, left and right are not changed

ï No loop

current is again computed as (left + right) // 2

Again consider position data[current]

If searched is not found, call the function recursively and either adjust leftor right accordingly

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 15 / 27

Page 89: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Recursive Implementation

Function again takes parameters data and for the given list and thesearched element

Two parameters left and right define the current search space

In a single call, left and right are not changed

ï No loop

current is again computed as (left + right) // 2

Again consider position data[current]

If searched is not found, call the function recursively and either adjust leftor right accordingly

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 15 / 27

Page 90: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Exercise – Recursive Binary Search

Implement binary searchas a recursive Python functionwith four parametersdata, left, right, and searchedFollow the ideas of the iterative variant

def binsearch(data, searched):left = 0right = len(data) - 1while left <= right:

current = (left + right) // 2if data[current] == searched:

return currentelif data[current] > searched:

right = current - 1else:

left = current + 1return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 16 / 27

Page 91: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

def binsearch(data, left, right, searched):

if left <= right:current = (left + right) // 2if data[current] == searched:

return currentelif data[current] > searched:

return binsearch(data, left, current-1, searched)else:

return binsearch(data, current+1, right, searched)else:

return -1

def binsearch(data, searched):left = 0right = len(data) - 1while left <= right:

current = (left + right) // 2if data[current] == searched:

return currentelif data[current] > searched:

right = current - 1else:

left = current + 1

return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 17 / 27

Page 92: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

def binsearch(data, left, right, searched):

if left <= right:current = (left + right) // 2if data[current] == searched:

return currentelif data[current] > searched:

return binsearch(data, left, current-1, searched)else:

return binsearch(data, current+1, right, searched)else:

return -1

def binsearch(data, searched):left = 0right = len(data) - 1while left <= right:

current = (left + right) // 2if data[current] == searched:

return currentelif data[current] > searched:

right = current - 1else:

left = current + 1

return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 17 / 27

Page 93: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 94: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 95: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)current = 6,recursive call

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 96: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 10, 12, 45)

current = 6,recursive call

current = 9,recursive call

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 97: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 10, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 12, 12, 45)

current = 6,recursive call

current = 9,recursive call

current = 11,recursive call

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 98: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 10, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 12, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 13, 12, 45)

current = 6,recursive call

current = 9,recursive call

current = 11,recursive call

current = 12,recursive call

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 99: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 10, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 12, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 13, 12, 45)

current = 6,recursive call

current = 9,recursive call

current = 11,recursive call

current = 12,recursive call

return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 100: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 10, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 12, 12, 45)

current = 6,recursive call

current = 9,recursive call

current = 11,recursive call

return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 101: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 10, 12, 45)

current = 6,recursive call

current = 9,recursive call

return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 102: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45)

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 7, 12, 45)current = 6,recursive call

return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 103: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Binary Search

Call Stack

binsearch([2, 3, 5, 8, 10, 19, 21, 25, 28, 32, 36, 37, 42], 0, 12, 45) return -1

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27

Page 104: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Sorting and SearchingO(n log n) Sorting Algorithms

Page 105: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

[8, 3, 1, 5, 6, 2, 4, 7][[8], [3], [1], [5], [6], [2], [4], [7]][[3, 8], [1, 5], [2, 6], [4, 7]][[1, 3, 5, 8], [2, 4, 6, 7]][[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 19 / 27

Page 106: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

[8, 3, 1, 5, 6, 2, 4, 7]

[[8], [3], [1], [5], [6], [2], [4], [7]][[3, 8], [1, 5], [2, 6], [4, 7]][[1, 3, 5, 8], [2, 4, 6, 7]][[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 19 / 27

Page 107: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

[8, 3, 1, 5, 6, 2, 4, 7]

[[8], [3], [1], [5], [6], [2], [4], [7]]

[[3, 8], [1, 5], [2, 6], [4, 7]][[1, 3, 5, 8], [2, 4, 6, 7]][[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 19 / 27

Page 108: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

[8, 3, 1, 5, 6, 2, 4, 7][[8], [3], [1], [5], [6], [2], [4], [7]]

[[3, 8], [1, 5], [2, 6], [4, 7]]

[[1, 3, 5, 8], [2, 4, 6, 7]][[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 19 / 27

Page 109: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

[8, 3, 1, 5, 6, 2, 4, 7][[8], [3], [1], [5], [6], [2], [4], [7]][[3, 8], [1, 5], [2, 6], [4, 7]]

[[1, 3, 5, 8], [2, 4, 6, 7]]

[[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 19 / 27

Page 110: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

[8, 3, 1, 5, 6, 2, 4, 7][[8], [3], [1], [5], [6], [2], [4], [7]][[3, 8], [1, 5], [2, 6], [4, 7]][[1, 3, 5, 8], [2, 4, 6, 7]]

[[1, 2, 3, 4, 5, 6, 7, 8]]

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 19 / 27

Page 111: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

Centerpiece is the function merge which merges two sorted lists

def merge(left, right):result = []while len(left) > 0 and len(right) > 0:

if left[0] > right[0]:result.append(right.pop(0))

else:result.append(left.pop(0))

return result + left + right

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 20 / 27

Page 112: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Iterative Mergesort

Centerpiece is the function merge which merges two sorted lists

def merge(left, right):result = []while len(left) > 0 and len(right) > 0:

if left[0] > right[0]:result.append(right.pop(0))

else:result.append(left.pop(0))

return result + left + right

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 20 / 27

Page 113: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 114: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 115: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

recursivecall

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 116: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

recursivecall

recursivecall

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 117: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

recursivecall

recursivecall

returnand merge

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 118: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

recursivecall

returnand merge

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 119: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

8 3 1 5 6 2 4 7

3 8 1 5 2 6 4 7

1 3 5 8 2 4 6 7

1 2 3 4 5 6 7 8

returnand merge

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 21 / 27

Page 120: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Exercise – Recursive Mergesort

Implement Mergesort

as a recursive Python function

that takes a list as parameter

splits it in the middle into two lists

calls the algorithm recursively onthese lists

merges the lists that are sortedthis way, and returns them

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 22 / 27

Page 121: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Mergesort

def mergesort(data):if len(data) <= 1:

return datamid = len(data) // 2leftdata = mergesort(data[:mid])rightdata = mergesort(data[mid:])result = []while len(leftdata) > 0 and len(rightdata) > 0:

if leftdata[0] > rightdata[0]:result.append(rightdata.pop(0))

else:result.append(leftdata.pop(0))

return result + leftdata + rightdata

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 23 / 27

Page 122: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Sorting and SearchingO(n log n) Sorting Algorithms – Quicksort

Page 123: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

One of the best-known sorting algorithms

Worst-case time complexity in O(n2)But can be randomized at a specific place

Expected time complexity in O(n log n)Very good time complexity in practice

Pick arbitrary pivot element (we always take the first one)

Create a list with smaller and one with larger elements

Call algorithm recursively on these lists

Concatenate lists that are sorted this way

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 24 / 27

Page 124: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

One of the best-known sorting algorithms

Worst-case time complexity in O(n2)

But can be randomized at a specific place

Expected time complexity in O(n log n)Very good time complexity in practice

Pick arbitrary pivot element (we always take the first one)

Create a list with smaller and one with larger elements

Call algorithm recursively on these lists

Concatenate lists that are sorted this way

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 24 / 27

Page 125: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

One of the best-known sorting algorithms

Worst-case time complexity in O(n2)But can be randomized at a specific place

Expected time complexity in O(n log n)Very good time complexity in practice

Pick arbitrary pivot element (we always take the first one)

Create a list with smaller and one with larger elements

Call algorithm recursively on these lists

Concatenate lists that are sorted this way

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 24 / 27

Page 126: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

One of the best-known sorting algorithms

Worst-case time complexity in O(n2)But can be randomized at a specific place

Expected time complexity in O(n log n)Very good time complexity in practice

Pick arbitrary pivot element (we always take the first one)

Create a list with smaller and one with larger elements

Call algorithm recursively on these lists

Concatenate lists that are sorted this way

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 24 / 27

Page 127: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

One of the best-known sorting algorithms

Worst-case time complexity in O(n2)But can be randomized at a specific place

Expected time complexity in O(n log n)Very good time complexity in practice

Pick arbitrary pivot element (we always take the first one)

Create a list with smaller and one with larger elements

Call algorithm recursively on these lists

Concatenate lists that are sorted this way

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 24 / 27

Page 128: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

One of the best-known sorting algorithms

Worst-case time complexity in O(n2)But can be randomized at a specific place

Expected time complexity in O(n log n)Very good time complexity in practice

Pick arbitrary pivot element (we always take the first one)

Create a list with smaller and one with larger elements

Call algorithm recursively on these lists

Concatenate lists that are sorted this way

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 24 / 27

Page 129: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

51

3

2

7

3

1

4

8

5

6

6

4

7

2

8

331 12 43 24 5Pivot 1

776 87 68

111 22 3Pivot 2.1

44 66 7Pivot 2.2

88

1Pivot 3

22 4 6 8

2

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 130: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

3

31

1

2

4

3

2

4

5Pivot 1

7

76

8

7

6

8

111 22 3Pivot 2.1

44 66 7Pivot 2.2

88

1Pivot 3

22 4 6 8

2

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 131: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

3

3

1

1

2

4

3

2

4

5Pivot 1

7

7

6

8

7

6

8

1

11

2

2

3Pivot 2.1

4

4

6

6

7Pivot 2.2

8

8

1Pivot 3

22 4 6 8

2

recursivecall

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 132: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

3

3

1

1

2

4

3

2

4

5Pivot 1

7

7

6

8

7

6

8

1

1

1

2

2

3Pivot 2.1

4

4

6

6

7Pivot 2.2

8

8

1Pivot 3

2

2

4 6 8

2

recursivecall

recursivecall

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 133: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

3

3

1

1

2

4

3

2

4

5Pivot 1

7

7

6

8

7

6

8

1

1

1

2

2

3Pivot 2.1

4

4

6

6

7Pivot 2.2

8

8

1Pivot 3

2

2

4 6 8

2

recursivecall

recursivecall

recursivecall

recursivecall

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 134: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

3

3

1

1

2

4

3

2

4

5Pivot 1

7

7

6

8

7

6

8

1

1

1

2

2

3Pivot 2.1

4

4

6

6

7Pivot 2.2

8

8

1Pivot 3

2

2 4 6 8

2

recursivecall

recursivecall

recursivecall

return

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 135: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

3

3

1

1

2

4

3

2

4

5Pivot 1

7

7

6

8

7

6

8

11

1

2

2 3Pivot 2.1

4

4

6

6 7Pivot 2.2

8

8

1Pivot 3

22 4 6 8

2

recursivecall

recursivecall

return andconcatenate

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 136: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

5

5

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

33

1

1

2

4

3

2

4 5Pivot 1

77

6

8

7

6

8

111 22 3Pivot 2.1

44 66 7Pivot 2.2

88

1Pivot 3

22 4 6 8

2

recursivecall

return andconcatenate

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 137: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

55

1

3

2

7

3

1

4

8

5

6

6

4

7

2

8

331 12 43 24 5Pivot 1

776 87 68

111 22 3Pivot 2.1

44 66 7Pivot 2.2

88

1Pivot 3

22 4 6 8

2

return andconcatenate

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 25 / 27

Page 138: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Exercise – Recursive Quicksort

Implement Quicksort

as a recursive Python functionthat takes a list datachooses the first element of data as pivotelementcreates a list with smaller and a list withlarger elementscalls the algorithm recursively on theselistsconcatenates and returns the lists that aresorted this way and the pivot element

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 26 / 27

Page 139: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Recursive Quicksort

def quicksort(data):if len(data) <= 1:

return dataelse:

pivot = data[0]leftdata = [i for i in data[1:] if i < pivot]rightdata = [i for i in data[1:] if i >= pivot]return quicksort(leftdata) + [pivot] + quicksort(rightdata)

Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 27 / 27

Page 140: New Dennis Komm Programming and Problem-Solving · 2020. 4. 25. · Recursive Functions deff(): ⇐⇒Python “learns” new word f From Merriam-Webster dictionary re ·frig er a·tor

Thanks for your attention


Recommended