Recursive Algorithms for Fibonacci Series

undefined
Recursion
Understand how the Fibonacci series is generated
Recursive Algorithms
Write simple recursive algorithms
Analyze simple recursive algorithms
Understand the drawbacks of recursion
Name other recursive algorithms and data
structures
John Edgar
2
What happens if you put a
pair of rabbits in a field?
More rabbits!
Assume that rabbits take
one month to reach
maturity and that
Each pair of rabbits
produces another pair of
rabbits one month after
mating.
John Edgar
3
How many pairs of rabbits
are there after 5 months?
Month 1: start – 
1
Month 2: the rabbits are now
mature and can mate – 
1
Month 3: – the first pair give
birth to two babies – 
2
Month 4: the first pair give birth
to 2 babies, the pair born in
month 3 are now mature – 
3
Month 5: the 3 pairs from
month 4, and 2 new pairs – 
5
John Edgar
4
 
 
 
 
 
 
 
A
f
t
e
r
 
5
 
m
o
n
t
h
s
 
t
h
e
r
e
 
a
r
e
 
5
p
a
i
r
s
 
o
f
 
r
a
b
b
i
t
s
i
.
e
.
 
t
h
e
 
n
u
m
b
e
r
 
o
f
 
p
a
i
r
s
 
a
t
 
4
m
o
n
t
h
s
 
(
3
)
 
p
l
u
s
 
t
h
e
 
n
u
m
b
e
r
 
o
f
p
a
i
r
s
 
a
t
 
3
 
m
o
n
t
h
s
 
(
2
)
W
h
y
?
W
h
i
l
e
 
t
h
e
r
e
 
a
r
e
 
3
 
p
a
i
r
s
 
o
f
b
u
n
n
i
e
s
 
i
n
 
m
o
n
t
h
 
4
 
o
n
l
y
 
2
 
o
f
t
h
e
m
 
a
r
e
 
a
b
l
e
 
t
o
 
m
a
t
e
t
h
e
 
o
n
e
s
 
a
l
i
v
e
 
i
n
 
m
o
n
t
h
 
3
T
h
i
s
 
s
e
r
i
e
s
 
o
f
 
n
u
m
b
e
r
s
 
i
s
c
a
l
l
e
d
 
t
h
e
 
F
i
b
o
n
a
c
c
i
 
s
e
r
i
e
s
John Edgar
5
month:
pairs:
1
2
3
4
5
6
1
1
2
3
5
8
 
 
 
 
 
 
 
 
T
h
e
 
n
t
h
 
n
u
m
b
e
r
 
i
n
 
t
h
e
 
F
i
b
o
n
a
c
c
i
 
s
e
r
i
e
s
,
 
f
i
b
(
n
)
,
 
i
s
:
0
 
i
f
 
n
 
=
 
0
,
 
a
n
d
 
1
 
i
f
 
n
 
=
 
1
f
i
b
(
n
 
 
1
)
 
+
 
f
i
b
(
n
 
 
2
)
 
 
f
o
r
 
a
n
y
 
n
 
>
 
1
e
.
g
.
 
w
h
a
t
 
i
s
 
f
i
b
(
2
3
)
E
a
s
y
 
i
f
 
w
e
 
o
n
l
y
 
k
n
e
w
 
f
i
b
(
2
2
)
 
a
n
d
 
f
i
b
(
2
1
)
T
h
e
 
a
n
s
w
e
r
 
i
s
 
f
i
b
(
2
2
)
 
+
 
f
i
b
(
2
1
)
W
h
a
t
 
h
a
p
p
e
n
s
 
i
f
 
w
e
 
a
c
t
u
a
l
l
y
 
w
r
i
t
e
 
a
 
f
u
n
c
t
i
o
n
 
t
o
 
c
a
l
c
u
l
a
t
e
F
i
b
o
n
a
c
c
i
 
n
u
m
b
e
r
s
 
l
i
k
e
 
t
h
i
s
?
John Edgar
6
Let's write a function just like the formula
fib(n) = 0 if n = 0, 1 if n = 1,
otherwise fib(n) = fib(n – 1) + fib(n – 2)
John Edgar
7
The function
calls itself
C++
The Fibonacci function is 
recursive
A recursive function calls itself
Each call to a recursive method results in a 
separate
 call to
the method, with its own input
Recursive functions are just like other functions
The invocation is pushed onto the call stack
And removed from the call stack when the end of a method
or a return statement is reached
Execution returns to the previous method call
John Edgar
8
int
 fib(
int
 n)
 
{
 
if
(n == 0 || n == 1)
  
return
 n;
 
else
  
return
 fib(n-1) + fib(n-2);
}
John Edgar
9
fib(5)
fib(4)
fib(3)
fib(3)
fib(2)
fib(1)
fib(0)
fib(2)
fib(1)
fib(0)
fib(1)
fib(2)
fib(1)
fib(0)
fib(1)
When a function is called it is pushed onto the call
stack
This applies to each invocation of that function
When a recursive call is made execution switches to
that
 method call
The call stack records the line number of the previous
method where the call was made from
Once a method call execution finishes, returns to the
previous invocation
John Edgar
10
undefined
 
January 2010
Greg Mori
11
Recursive functions do not use loops to repeat
instructions
But use recursive calls, in if statements
Recursive functions consist of two or more cases,
there must be at least one
Base case, and one
Recursive case
John Edgar
12
The base case is a smaller problem with a
simpler solution
This problem’s solution must 
not
 be recursive
Otherwise the function may never terminate
There can be more than one base case
John Edgar
13
The recursive case is the same problem with
smaller input
The recursive case must include a recursive
function call
There can be more than one recursive case
John Edgar
14
Define the problem in terms of a smaller
problem of the same type
The recursive part
e.g. 
return
 fib(n-1) + fib(n-2);
And the base case where the solution can be
easily calculated
This solution should not be recursive
e.g. 
if
 (n == 0 || n == 1) 
return
 n;
John Edgar
15
How can the problem be defined in terms of
smaller problems of the same type?
By how much does each recursive call reduce the
problem size?
By 1, by half, …?
What are the base cases that can be solved
without recursion?
Will a base case be reached as the problem size is
reduced?
John Edgar
16
undefined
 
January 2010
Greg Mori
17
Linear Search
Binary Search
Assume sorted array
John Edgar
18
John Edgar
19
C++
The algorithm searches the array one
element at a time using a for loop
Base cases
Target is found at first position in array
The end of the array is reached
Recursive case
Target not found at first position
 Search again, discarding the first element of the array
John Edgar
20
John Edgar
21
C++
Of course, if it’s a sorted array we wouldn’t do
linear search
John Edgar
22
Each sub-problem searches a subarray
Differs only in the upper and lower array indices
that define the subarray
Each sub-problem is smaller than the last one
In the case of binary search, half the size
There are two base cases
When the target item is found and
When the problem space consists of one item
Make sure that this last item is checked
John Edgar
23
John Edgar
24
C++
undefined
 
January 2010
Greg Mori
25
Merge Sort
Quicksort
John Edgar
26
undefined
 
January 2010
Greg Mori
27
 
What’s the easiest list to sort?
A list of 1 number
John Edgar
28
Let’s say I have 2 sorted lists of numbers
How can I merge them into 1 sorted list?
John Edgar
29
1
3
5
12
22
23
42
99
output
1
12
22
23
3
5
42
99
List 1
List 2
If I have a list of n numbers, how should I sort
them?
I know two things
How to sort a list of 1 number
How to merge 2 sorted lists of numbers into 1
sorted list
Smells like recursion
John Edgar
30
John Edgar
31
What is the time complexity of a merge?
John Edgar
32
1
3
5
12
22
23
42
99
output
1
12
22
23
3
5
42
99
List 1
List 2
How many recursive steps are there?
How large are the merges at each recursive
step?
Merge takes O(n) time for n elements
John Edgar
33
John Edgar
34
John Edgar
35
How many recursive steps are there?
How large are the merges at each recursive
step?
Merge takes O(n) time for n elements
John Edgar
36
How many recursive steps are there?
O(log n) steps: split array in half each time
How large are the merges at each recursive step?
In total, merge n elements each step
Time complexity is O(n log n)
Mergesort
Best case: 
O
(
n
(log
2
n
))
Average case: 
O
(
n
(log
2
n
))
Worst case: 
O
(
n
(log
2
n
))
John Edgar
37
undefined
 
Quicksort is a more efficient sorting algorithm than
either selection or insertion sort
It sorts an array by repeatedly 
partitioning
 it
We will go over the basic idea of quicksort and an
example of it
See text / on-line resources for details
John Edgar
39
Partitioning is the process of dividing an array into
sections (partitions), based on some criteria
"Big" and "small" values
Negative and positive numbers
Names that begin with 
a
-
m
, names that begin with 
n
-
z
Darker and lighter pixels
Quicksort uses repeated partitioning to sort an
array
John Edgar
40
John Edgar
41
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
31
12
07
23
93
02
11
18
John Edgar
42
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
31
12
07
23
93
02
11
18
18
John Edgar
43
31
12
07
23
93
02
11
18
We will partition the array
around the last value (18), we'll
call this value the 
pivot
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
John Edgar
44
31
12
07
23
93
02
11
18
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
John Edgar
45
31
12
07
23
93
02
11
18
31
11
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
John Edgar
46
12
07
23
93
02
18
repeat this process until:
31
23
02
11
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
12
07
John Edgar
47
12
07
93
18
repeat this process until:
31
23
02
11
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
John Edgar
48
repeat this process until:
high and low are the same
We'd like the pivot value to be in the
centre of the array, so we will swap it
with the first item greater than it
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
12
07
93
18
31
23
02
11
93
18
John Edgar
49
 
smalls
 
bigs
 
pivot
Partition this array into 
small
and 
big
 values using a
partitioning algorithm
We will partition the array
around the last value (18), we'll
call this value the 
pivot
12
07
93
18
31
23
02
11
John Edgar
50
Use the same algorithm to
partition this array into small
and big values
00
08
07
01
06
02
05
09
 
bigs!
 
pivot
00
08
07
01
06
02
05
09
 
smalls
John Edgar
51
Or this one:
09
08
07
06
05
04
02
01
 
bigs
 
pivot
01
08
07
06
05
04
02
09
 
smalls
The quicksort algorithm works by 
repeatedly
partitioning
 an array
Each time a subarray is partitioned there is
A sequence of 
small
 values,
A sequence of 
big
 values, and
A 
pivot
 value 
which is in the correct position
Partition the small values, and the big values
Repeat the process until each subarray being partitioned
consists of just one element
John Edgar
52
How long does quicksort take to run?
Let's consider the best and the worst case
These differ because the partitioning algorithm may not
always do a good job
Let's look at the best case first
Each time a subarray is partitioned the pivot is the exact
midpoint of the slice (or as close as it can get)
So it is divided in half
What is the running time?
John Edgar
53
John Edgar
54
08
01
02
07
03
06
04
05
 
bigs
 
pivot
04
01
02
03
05
06
08
07
 
smalls
First partition
John Edgar
55
 
big1
 
pivot1
02
01
04
05
06
08
 
sm1
04
01
02
03
05
06
08
07
Second partition
07
03
 
pivot1
 
pivot2
 
pivot2
 
big2
 
sm2
John Edgar
56
 
pivot1
02
03
04
05
06
07
08
Third partition
02
01
03
04
05
06
07
08
 
pivot1
 
done
 
done
 
done
01
Each subarray is divided exactly in half in each set of
partitions
Each time a series of subarrays are partitioned around 
n
comparisons are made
The process ends once all the subarrays left to be partitioned
are of size 1
How many times does 
n
 have to be divided in half
before the result is 1?
log
2
 (
n
) times
Quicksort performs around 
n
 * log
2
 (
n
) operations in the best
case
John Edgar
57
John Edgar
58
09
08
07
06
05
04
02
01
 
bigs
 
pivot
01
08
07
06
05
04
02
09
 
smalls
First partition
John Edgar
59
 
bigs
 
pivot
01
08
07
06
05
04
02
09
 
smalls
01
08
07
06
05
04
02
09
Second partition
John Edgar
60
 
bigs
 
pivot
01
02
07
06
05
04
08
09
Third partition
01
08
07
06
05
04
02
09
John Edgar
61
 
pivot
01
02
07
06
05
04
08
09
 
smalls
Fourth
partition
01
02
07
06
05
04
08
09
John Edgar
62
 
bigs
 
pivot
01
02
04
06
05
07
08
09
Fifth partition
01
02
07
06
05
04
08
09
John Edgar
63
 
pivot
01
02
04
06
05
07
08
09
 
smalls
Sixth partition
01
02
04
06
05
07
08
09
John Edgar
64
01
02
04
05
06
07
08
09
Seventh(!)
partition
01
02
04
06
05
07
08
09
Every partition step results in just one
partition on one side of the pivot
The array has to be partitioned 
n
 times, not
log
2
(
n
) times
So in the worst case quicksort performs around 
n
2
operations
The worst case usually occurs when the array
is nearly sorted (in either direction)
John Edgar
65
With a large array we would have to be very,
very unlucky to get the worst case
Unless there was some reason for the array to
already be partially sorted
In which case first randomize the position of the array
elements!
The average case is much more like the best case
than the worst case
John Edgar
66
undefined
 
January 2010
Greg Mori
67
Recursive algorithms have more overhead
than similar iterative algorithms
Because of the repeated method calls
This may cause a 
stack overflow
 when the call
stack gets full
It is often useful to derive a solution using
recursion and implement it iteratively
Sometimes this can be quite challenging!
John Edgar
68
Some recursive algorithms are inherently
inefficient
e.g. the recursive Fibonacci algorithm which
repeats the same calculation again and again
Look at the number of times
 fib(2)
 is called
Such algorithms should be 
implemented
iteratively
Even if the solution was 
determined
 using recursion
John Edgar
69
It is useful to trace through the sequence of
recursive calls
This can be done using a recursion tree
Recursion trees can be used to determine the
running time of algorithms
Annotate the tree to indicate how much work is
performed at each level of the tree
And then determine how many levels of the tree
there are
John Edgar
70
undefined
 
January 2010
Greg Mori
71
Recursion is similar to induction
Recursion 
solves
 a problem by
Specifying a solution for the base case and
Using a recursive case to derive solutions of any
size from solutions to smaller problems
Induction 
proves
 a property by
Proving it is true for a base case and
Proving that it is true for some number, 
n
, if it is
true for all numbers less than 
n
John Edgar
72
Prove, using induction that the algorithm returns
the values
fact
(0) = 0! =1
fact
(n) = 
n
! = 
n
 * (
n
 – 1) * … * 1 if 
n
 > 0
John Edgar
73
C++
Basis:
 Show that the property is true for 
n
 = 0, i.e.
that
 fact
(0) returns 1
This is true by definition as 
fact
(0) is the base case of the
algorithm and returns 1
Establish that the property is true for an arbitrary 
k
implies that it is also true for 
k
 + 1
Inductive hypothesis:
 Assume that the property is
true for 
n
 = 
k
, that is assume that
fact
(
k
) = 
k
 * (
k
 – 1) * (
k
 – 2) * … * 2 * 1
John Edgar
74
 
 
 
 
 
 
 
 
 
 
I
n
d
u
c
t
i
v
e
 
c
o
n
c
l
u
s
i
o
n
:
 
S
h
o
w
 
t
h
a
t
 
t
h
e
 
p
r
o
p
e
r
t
y
 
i
s
t
r
u
e
 
f
o
r
 
n
 
=
 
k
 
+
 
1
,
 
i
.
e
.
,
 
t
h
a
t
 
f
a
c
t
(
k
 
+
 
1
)
 
r
e
t
u
r
n
s
(
k
 
+
 
1
)
 
*
 
k
 
*
 
(
k
 
 
1
)
 
*
 
(
k
 
 
2
)
 
*
 
 
*
 
2
 
*
 
1
B
y
 
d
e
f
i
n
i
t
i
o
n
 
o
f
 
t
h
e
 
f
u
n
c
t
i
o
n
:
 
f
a
c
t
(
k
 
+
 
1
)
 
r
e
t
u
r
n
s
(
k
 
+
 
1
)
 
*
 
f
a
c
t
(
k
)
 
 
t
h
e
 
r
e
c
u
r
s
i
v
e
 
c
a
s
e
A
n
d
 
b
y
 
t
h
e
 
i
n
d
u
c
t
i
v
e
 
h
y
p
o
t
h
e
s
i
s
:
 
f
a
c
t
(
k
)
 
r
e
t
u
r
n
s
k
 
*
 
(
k
 
 
1
)
 
*
 
(
k
 
 
2
)
 
*
 
 
*
 
2
 
*
 
1
T
h
e
r
e
f
o
r
e
 
 
f
a
c
t
(
k
 
+
 
1
)
 
m
u
s
t
 
r
e
t
u
r
n
(
k
 
+
 
1
)
 
*
 
k
 
*
 
(
k
 
 
1
)
 
*
 
(
k
 
 
2
)
 
*
 
 
*
 
2
 
*
 
1
W
h
i
c
h
 
c
o
m
p
l
e
t
e
s
 
t
h
e
 
i
n
d
u
c
t
i
v
e
 
p
r
o
o
f
John Edgar
75
Recursive sum
Towers of Hanoi – see text
Eight Queens problem – see text
Sorting
Mergesort
Quicksort
John Edgar
76
Linked Lists are recursive data structures
They are defined in terms of themselves
There are recursive solutions to many list
methods
List traversal can be performed recursively
Recursion allows elegant solutions of problems
that are hard to implement iteratively
Such as printing a list backwards
John Edgar
77
undefined
 
January 2010
Greg Mori
78
Recursion as a problem-solving tool
Identify base case where solution is simple
Formulate other cases in terms of smaller case(s)
Recursion is not always a good
implementation strategy
Solve the same problem many times
Function call overhead
Recursion and induction
Induction proves properties in a form similar to
how recursion solves problems
John Edgar
79
Carrano Ch. 2, 5
John Edgar
80
Slide Note
Embed
Share

The Fibonacci series, generated through recursive algorithms, explores the growth pattern of pairs of rabbits in a field. By understanding the drawbacks of recursion and analyzing simple recursive algorithms, we delve into the essence of Fibonacci numbers. Discover how the series evolves each month as the rabbits multiply. Explore the recursive approach to calculating Fibonacci numbers in C++, highlighting the self-calling function. Uncover the beauty and complexity of recursive computations reflected in the Fibonacci sequence.

  • Fibonacci Series
  • Recursive Algorithms
  • Rabbit Growth
  • Computational Patterns
  • C++

Uploaded on Feb 18, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Recursion

  2. Understand how the Fibonacci series is generated Recursive Algorithms Write simple recursive algorithms Analyze simple recursive algorithms Understand the drawbacks of recursion Name other recursive algorithms and data structures John Edgar 2

  3. What happens if you put a pair of rabbits in a field? More rabbits! Assume that rabbits take one month to reach maturity and that Each pair of rabbits produces another pair of rabbits one month after mating. John Edgar 3

  4. How many pairs of rabbits are there after 5 months? Month 1: start 1 Month 2: the rabbits are now mature and can mate 1 Month 3: the first pair give birth to two babies 2 Month 4: the first pair give birth to 2 babies, the pair born in month 3 are now mature 3 Month 5: the 3 pairs from month 4, and 2 new pairs 5 John Edgar 4

  5. month: month: 1 1 2 2 3 3 4 4 5 5 6 6 After 5 months there are 5 pairs of rabbits i.e. the number of pairs at 4 months (3) plus the number of pairs at 3 months (2) Why? While there are 3 pairs of bunnies in month 4 only 2 of them are able to mate the ones alive in month 3 This series of numbers is called the Fibonacci series pairs: pairs: 1 1 1 1 2 2 3 3 5 5 8 8 John Edgar 5

  6. The nth number in the Fibonacci series, fib(n), is: 0 if n = 0, and 1 if n = 1 fib(n 1) + fib(n 2) for any n > 1 e.g. what is fib(23) Easy if we only knew fib(22) and fib(21) The answer is fib(22) + fib(21) What happens if we actually write a function to calculate Fibonacci numbers like this? John Edgar 6

  7. C++ Let's write a function just like the formula fib(n) = 0 if n = 0, 1 if n = 1, otherwise fib(n) = fib(n 1) + fib(n 2) int fib(int n){ if(n == 0 || n == 1){ return n; }else{ return fib(n-1) + fib(n-2); } } The function calls itself John Edgar 7

  8. The Fibonacci function is recursive A recursive function calls itself Each call to a recursive method results in a separate call to the method, with its own input Recursive functions are just like other functions The invocation is pushed onto the call stack And removed from the call stack when the end of a method or a return statement is reached Execution returns to the previous method call John Edgar 8

  9. int fib(int n) if(n == 0 || n == 1) return n; else return fib(n-1) + fib(n-2); } { 5 fib(5) 2 3 fib(3) fib(4) 1 1 2 1 fib(3) fib(2) fib(2) fib(1) 1 1 1 0 1 0 fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1 0 fib(1) fib(0) John Edgar 9

  10. When a function is called it is pushed onto the call stack This applies to each invocation of that function When a recursive call is made execution switches to that method call The call stack records the line number of the previous method where the call was made from Once a method call execution finishes, returns to the previous invocation John Edgar 10

  11. January 2010 Greg Mori 11

  12. Recursive functions do not use loops to repeat instructions But use recursive calls, in if statements Recursive functions consist of two or more cases, there must be at least one Base case, and one Recursive case John Edgar 12

  13. The base case is a smaller problem with a simpler solution This problem s solution must not be recursive Otherwise the function may never terminate There can be more than one base case John Edgar 13

  14. The recursive case is the same problem with smaller input The recursive case must include a recursive function call There can be more than one recursive case John Edgar 14

  15. Define the problem in terms of a smaller problem of the same type The recursive part e.g. return fib(n-1) + fib(n-2); And the base case where the solution can be easily calculated This solution should not be recursive e.g. if (n == 0 || n == 1) return n; John Edgar 15

  16. How can the problem be defined in terms of smaller problems of the same type? By how much does each recursive call reduce the problem size? By 1, by half, ? What are the base cases that can be solved without recursion? Will a base case be reached as the problem size is reduced? John Edgar 16

  17. January 2010 Greg Mori 17

  18. Linear Search Binary Search Assume sorted array John Edgar 18

  19. C++ int linSearch(int *arr, int n, int x){ for (int i=0; i < n; i++){ if(x == arr[i]){ return i; } } //for return -1; //target not found } The algorithm searches the array one element at a time using a for loop John Edgar 19

  20. Base cases Target is found at first position in array The end of the array is reached Recursive case Target not found at first position Search again, discarding the first element of the array John Edgar 20

  21. C++ int linSearch(int *arr, int n, int x){ return recLinSearch(arr,n,0,x); } int recLinSearch(int *arr, int n, int i, int x){ if (i >= n){ return -1; } else if (x == arr[i]){ return i; } else return recLinSearch(arr, n, i + 1, x); } } John Edgar 21

  22. Of course, if its a sorted array we wouldnt do linear search John Edgar 22

  23. Each sub-problem searches a subarray Differs only in the upper and lower array indices that define the subarray Each sub-problem is smaller than the last one In the case of binary search, half the size There are two base cases When the target item is found and When the problem space consists of one item Make sure that this last item is checked John Edgar 23

  24. C++ int binSearch(int *arr, int lower, int upper, int x){ int mid = (lower + upper) / 2; if (lower > upper){ return - 1; //base case } else if(arr[mid] == x){ return mid; //second base case } else if(arr[mid] < x){ return binSearch(arr, mid + 1, upper, x); } else { //arr[mid] > target return binSearch(arr, lower, mid - 1, x); } } John Edgar 24

  25. January 2010 Greg Mori 25

  26. Merge Sort Quicksort John Edgar 26

  27. January 2010 Greg Mori 27

  28. Whats the easiest list to sort? A list of 1 number John Edgar 28

  29. Lets say I have 2 sorted lists of numbers How can I merge them into 1 sorted list? 1 12 22 23 3 5 42 99 List 1 List 2 1 3 5 12 22 23 42 99 output John Edgar 29

  30. If I have a list of n numbers, how should I sort them? I know two things How to sort a list of 1 number How to merge 2 sorted lists of numbers into 1 sorted list Smells like recursion John Edgar 30

  31. mergeSort (array) if (array is length 1) // base case, one element return the array else arr1 = mergeSort(first half of array) arr2 = mergeSort(second half of array) return merge(arr1,arr2) John Edgar 31

  32. What is the time complexity of a merge? 1 12 22 23 3 5 42 99 List 1 List 2 1 3 5 12 22 23 42 99 output John Edgar 32

  33. How many recursive steps are there? How large are the merges at each recursive step? Merge takes O(n) time for n elements John Edgar 33

  34. 23 07 41 11 33 19 81 23 07 33 19 41 11 45 45 81 Sort entire array Sorted entire array 23 23 41 33 33 41 81 81 07 07 19 11 11 19 45 45 Sort halves Sorted halves 23 23 41 41 33 33 81 81 07 07 19 19 11 11 45 45 Sort quarters Sorted quarters 23 41 33 81 07 19 11 45 Sort eighths John Edgar 34

  35. 23 41 33 81 07 19 11 45 Sort entire array 23 41 33 81 07 19 11 45 Sort halves 23 41 33 81 07 19 11 45 Sort quarters 23 41 33 81 07 19 11 45 Sort eighths How many recursive steps are there? How large are the merges at each recursive step? Merge takes O(n) time for n elements John Edgar 35

  36. 23 41 33 81 07 19 11 45 Sort entire array 23 41 33 81 07 19 11 45 Sort halves 23 41 33 81 07 19 11 45 Sort quarters 23 41 33 81 07 19 11 45 Sort eighths How many recursive steps are there? O(log n) steps: split array in half each time How large are the merges at each recursive step? In total, merge n elements each step Time complexity is O(n log n) John Edgar 36

  37. Mergesort Best case: O(n(log2n)) Average case: O(n(log2n)) Worst case: O(n(log2n)) John Edgar 37

  38. Quicksort is a more efficient sorting algorithm than either selection or insertion sort It sorts an array by repeatedly partitioning it We will go over the basic idea of quicksort and an example of it See text / on-line resources for details John Edgar 39

  39. Partitioning is the process of dividing an array into sections (partitions), based on some criteria "Big" and "small" values Negative and positive numbers Names that begin with a-m, names that begin with n-z Darker and lighter pixels Quicksort uses repeated partitioning to sort an array John Edgar 40

  40. Partition this array into small and big values using a partitioning algorithm 31 12 07 23 93 02 11 18 John Edgar 41

  41. Partition this array into small and big values using a partitioning algorithm 31 12 07 23 93 02 11 18 18 We will partition the array around the last value (18), we'll call this value the pivot 18 smalls < 18 smalls < 18 bigs bigs > 18 > 18 pivot pivot John Edgar 42

  42. Partition this array into small and big values using a partitioning algorithm 31 12 07 23 93 02 11 18 We will partition the array around the last value (18), we'll call this value the pivot arr[low ] is greater than the pivot and should be on the right, we need to swap it with something Use two indices, one at each end of the array, call them low and high John Edgar 43

  43. Partition this array into small and big values using a partitioning algorithm 31 12 07 23 93 02 11 18 We will partition the array around the last value (18), we'll call this value the pivot arr[low ] (31) is greater than the pivot and should be on the right, we need to swap it with something Use two indices, one at each end of the array, call them low and high arr[high] (11) is less than the pivot so swap with arr[low ] John Edgar 44

  44. Partition this array into small and big values using a partitioning algorithm 31 12 07 23 93 02 11 18 11 31 We will partition the array around the last value (18), we'll call this value the pivot Use two indices, one at each end of the array, call them low and high John Edgar 45

  45. Partition this array into small and big values using a partitioning algorithm 11 12 07 23 93 02 02 12 07 23 31 18 We will partition the array around the last value (18), we'll call this value the pivot repeat this process until: Use two indices, one at each end of the array, call them low and high John Edgar 46

  46. Partition this array into small and big values using a partitioning algorithm 11 12 07 02 93 23 31 18 We will partition the array around the last value (18), we'll call this value the pivot repeat this process until: high and low are the same Use two indices, one at each end of the array, call them low and high John Edgar 47

  47. Partition this array into small and big values using a partitioning algorithm 11 12 07 02 93 18 23 31 18 93 We will partition the array around the last value (18), we'll call this value the pivot repeat this process until: high and low are the same Use two indices, one at each end of the array, call them low and high We'd like the pivot value to be in the centre of the array, so we will swap it with the first item greater than it John Edgar 48

  48. Partition this array into small and big values using a partitioning algorithm 11 12 07 02 18 23 31 93 smalls smalls bigs bigs pivot pivot We will partition the array around the last value (18), we'll call this value the pivot Use two indices, one at each end of the array, call them low and high John Edgar 49

  49. 00 08 07 01 06 02 05 09 Use the same algorithm to partition this array into small and big values 00 08 07 01 06 02 05 09 smalls smalls bigs bigs! ! pivot pivot John Edgar 50

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#