Sei sulla pagina 1di 32

1

CS1102 Tut 4 Recursion and


Big Oh
Max Tan
tanhuiyi@comp.nus.edu.sg
S15-03-07 Tel:65164364
http://www.comp.nus.edu.sg/~tanhuiyi
2
Groups Assignment
Question 1 Group 1
Question 2 Group 2
Question 3 Group 3
Question 4 Group 4
Question 5 Group 1
Question 6 Group 2
Question 7 Group 3

3

4
First 15 minutes
Recap on MergeSort and QuickSort
Insertion sort, selection sort, bubble
sort are very simple O(n
2
) algorithms.
5
First 15 minutes
How does mergeSort work?

MergeSort(A[i..j])
Precondition : Nil
Postcondition : A[i..j] is sorted

6
First 15 minutes
MergeSort(A[i..j])
Precondition : Nil
Postcondition : A[i..j] is sorted

MergeSort on leftHalf
MergeSort on rightHalf
Merge 2 sorted halves.

7
First 15 minutes
Unsorted array
mergeSort : Sorts an imput array
Unsorted array
divide
mergeSort mergeSort
After mergesort is done, array is sorted!
8
First 15 minutes
Unsorted array
mergeSort : Sorts an imput array
Sorted arrays
divide
mergeSort mergeSort
9
First 15 minutes
Unsorted array
mergeSort : Sorts an imput array
Sorted arrays
divide
mergeSort mergeSort
Merge
10
First 15 minutes
Unsorted array
mergeSort : Sorts an imput array
Sorted arrays
divide
mergeSort mergeSort
Merge
Output
11
First 15 minutes
Time complexity analysis

h
Assume the height of this
tree is h
Number of elements in each
array in first level = n
Number of elements in each
array in 2
nd
level = n/2
in k
th
level = n/2
k-1
In the h
th
level, n/2
h-1
= 1
n = 2
h-1
lg n = h-1
h = lg n + 1

12
First 15 minutes
Time complexity analysis

h
h = lg n + 1
Each level has O(n)
computations
Hence h levels has O(hn)
computations
h = log n, therefore
Time complexity is O(nlog n)
13
First 15 minutes
QuickSort(A[i..j])
Precondition : Nil
Postcondition : A[i..j] is sorted

Find a pivot and position the elements
accordingly
QuickSort on left
QuickSort on right

14
First 15 minutes
Unsorted array
quickSort : Sorts an imput array
Find pivot
pivot
quickSort
quickSort
15
First 15 minutes
Unsorted array
quickSort : Sorts an imput array
Find pivot
pivot
quickSort
quickSort
16
First 15 minutes
QuickSort

QuickSort(A[i..j])
Precondition : Nil
Postcondition : A[i..j] is sorted



17
Question 1
Given the following recursive function:
int f(int n, int m) {
if (n == 0) return m;
else return f(n-1, 1-m);
}
What is the result of f(3,7)?
(a) 0
(b) 3
(c) 7
(d) -7
(e) None of the above
18
Question 1Solution
(e)

f(3,7)
= f(2,-6)
= f(1, 7)
= f(0, -6)
= -6
19
Question 2
Given the following recursive function:
int g(int n) {
if (n == 0) return 1
else return g(n-1) + g(n-2);
}
Which of the following statement is true?
(a) It does not have a base case
(b) It is not recursive
(c) The function does not terminate for all n
(d) None of the above
20
Question 2
Solution
(d)
g(n) where n is 0 terminates
As g(0) = 1
All other values do not terminate.
g(-1) does not terminate, so g(1) also does not
terminate
21
Question 3
Consider the following function where the input n is
assumed to be positive integer.
f(n) = n
2
+ (n-1)
2
+ + 2
2
+ 1
2

Write a recursive function to implement it and a main()
function to display the first 10 function values. What if n
is not positive?
22
Question 3
Solution
int f(int n) {
if (n <=0) return 0; // To be defensive
return n*n + f(n-1);
}
void main(String[] args) {
System.out.println(n\tf(n));
for (int i = 1; i <=10; ++i) {
System.out.println(i + \t + f(i));
}
}
23
Question 4
int h(int n) {
if (n <= 3) return 1;
else return 1 + h(n-1) + h(n-3);
}
(i) Compute the value of h(10)
(ii) Explain why the given (original) definition of h
is inefficient. Based on the above observation
(about how to compute a h call efficiently), show
how you can provide a more efficient h
implementation.
24
Question 4
Solution (1)
h(10) = 1 + h(9) + h(7)
h(9) = 1 + h(8) + h(6)
h(8) = 1 + h(7) + h(5)
h(7) = 1 + h(6) + h(4)
h(6) = 1 + h(5) + h(3)
h(5) = 1 + h(4) + h(2)
h(4) = 1 + h(3) + h(1)
h(3) = 1
h(2) = 1
h(1) = 1

The same computations are repeatedly executed when it
is called with the same value and hence the recursive
solution is inefficient
25
Question 4
Solution (1)
To improve its performance, use iterative solution.
int h(int n) {
if (n <=3) return 1;
int p1=1; int p2=1; int p3=1;
int p;
for (int i = 4; i <= n; i++) {
p = 1 + p1 + p3;
p3 = p2;
p2 = p1;
p1 = p;
}
return p;
}
26
Question 4
Solution (3)
compute h(0), h(1), , until h(10) will be easier to get the
answer
h(0) = 1
h(1) = 1
h(2) = 1
h(3) = 1
h(4) = 3
h(5) = 5
h(6) = 7
h(7) = 11
h(8) = 17
h(9) = 25
h(10) = 37
27
Question 5
int method( int n) {
if (n < = 1) return 1;
return method(n/2) + method(1);
}
The tightest time complexity of this method is:
(a) O(log n)
(b) O(n)
(c) O(n
2
)
(d) O(2
n
)
(e) O(n2
n
)
28
Question 5
Solution
(a)
Domain of interest is reduced by half for each
recursive call.
29
Question 6
What is the time complexity of each of the following tasks in the
worst case?
a. Computing the sum of the first n even integers by using a for
loop
b. Displaying all n integers in an array
c. Displaying all n integers in a sorted linked list
d. Displaying all n names in a circular linked list
e. Displaying one array element
f. Displaying the last integer in a linked list
g. Searching a sorted array of n integers for a particular value
by using a binary search
h. Adding an item to a stack of n items
i. Adding an item to a queue of n items
30
Question 6
Solution
a. Computing the sum of the first n even integers
by using a for loop
b. Displaying all n integers in an array
c. Displaying all n integers in a sorted linked list
d. Displaying all n names in a circular linked list
e. Displaying one array element
f. Displaying the last integer in a linked list
g. Searching an array of n integers for a
particular value by using a binary search
h. Adding an item to a stack of n items
i. Adding an item to a queue of n items
a. O(n)
b. O(n)
c. O(n)
d. O(n)
e. O(1)
f. O(n)
g. O( log n)
h. O(1)
i. O(1)
31
Question 7
Consider the following method f, which calls the method swap.
Assume that swap exists and simply swaps the contents of the
two arguments. Do not be concerned with fs purpose.
void f(int [] theArray, int n) {
for (int j = 0; j < n; ++j) {
int i = 0;
while (i < 1) {
if (theArray[i] < theArray[j])
swap(theArray[i], theArray[j]);
++i;
} // end while
} // end for
} // end f
How many comparisons does f perform?
32
Question 7
Solution
O(n)

as the statements in the while loop only execute once
for each j because of the condition i<1.

Potrebbero piacerti anche