![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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/38.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/39.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/40.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/41.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/42.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/43.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/44.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/45.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/46.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/47.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/48.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/49.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/50.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/51.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/52.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/53.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/54.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/55.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/56.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/57.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/58.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/59.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/60.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/61.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/62.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/63.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/64.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/65.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/66.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/67.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/68.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/69.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/70.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/71.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/72.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/73.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/74.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/75.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/76.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/77.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/78.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/79.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/80.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/81.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/82.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/83.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/84.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/85.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/86.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/87.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/88.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/89.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/90.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/91.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/92.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/93.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/94.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/95.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/96.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/97.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/98.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/99.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/100.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/101.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/102.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/103.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/104.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/105.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/106.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/107.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/108.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/109.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/110.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/111.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/112.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/113.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/114.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/115.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/116.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/117.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/118.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/119.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/120.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/121.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/122.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/123.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/124.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/125.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/126.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/127.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/128.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/129.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/130.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/131.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/132.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/133.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/134.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/135.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/136.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/137.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/138.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/139.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022051808/600f01d2bbb5f37f033b7d0f/html5/thumbnails/140.jpg)
Thanks for your attention