Dennis Komm
Programming and Problem-SolvingRecursive Algorithms
Spring 2020 – April 23, 2020
Recursive Functions
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Factorial and Sum
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Euclid’s Algorithm Recursively
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recursive Sorting and SearchingBinary Search
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
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
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
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
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
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
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
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
Recursive Binary Search
Call Stack
Programming and Problem-Solving – Recursive Algorithms Spring 2020 Dennis Komm 18 / 27
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
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
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
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
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
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
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
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
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
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
Recursive Sorting and SearchingO(n log n) Sorting Algorithms
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recursive Sorting and SearchingO(n log n) Sorting Algorithms – Quicksort
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Thanks for your attention
Top Related