Cs 1102 Tutorial 5

Embed Size (px)

Citation preview

  • 8/12/2019 Cs 1102 Tutorial 5

    1/32

    1

    CS1102 Tut 4 Recursion and

    Big Oh

    Max Tan

    [email protected]

    S15-03-07 Tel:65164364http://www.comp.nus.edu.sg/~tanhuiyi

    mailto:[email protected]:[email protected]
  • 8/12/2019 Cs 1102 Tutorial 5

    2/32

    2

    Groups Assignment

    Question 1Group 1

    Question 2Group 2

    Question 3Group 3

    Question 4Group 4

    Question 5

    Group 1 Question 6Group 2

    Question 7Group 3

  • 8/12/2019 Cs 1102 Tutorial 5

    3/32

    3

  • 8/12/2019 Cs 1102 Tutorial 5

    4/32

    4

    First 15 minutes

    Recap on MergeSort and QuickSort

    Insertion sort, selection sort, bubble

    sort are very simple O(n2) algorithms.

  • 8/12/2019 Cs 1102 Tutorial 5

    5/32

    5

    First 15 minutes

    How does mergeSort work?

    MergeSort(A[i..j])

    Precondition : Nil

    Postcondition : A[i..j] is sorted

  • 8/12/2019 Cs 1102 Tutorial 5

    6/32

    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.

  • 8/12/2019 Cs 1102 Tutorial 5

    7/32

    7

    First 15 minutes

    Unsorted array

    mergeSort : Sorts an imput array

    Unsorted array

    dividemergeSort mergeSort

    After mergesort is done, array is sorted!

  • 8/12/2019 Cs 1102 Tutorial 5

    8/32

    8

    First 15 minutes

    Unsorted array

    mergeSort : Sorts an imput array

    Sorted arrays

    dividemergeSort mergeSort

  • 8/12/2019 Cs 1102 Tutorial 5

    9/32

    9

    First 15 minutes

    Unsorted array

    mergeSort : Sorts an imput array

    Sorted arrays

    dividemergeSort mergeSort

    Merge

  • 8/12/2019 Cs 1102 Tutorial 5

    10/32

    10

    First 15 minutes

    Unsorted array

    mergeSort : Sorts an imput array

    Sorted arrays

    dividemergeSort mergeSort

    Merge

    Output

  • 8/12/2019 Cs 1102 Tutorial 5

    11/32

    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 2ndlevel = n/2

    in kthlevel = n/2k-1

    In the hthlevel, n/2h-1= 1

    n = 2h-1

    lg n = h-1

    h = lg n + 1

  • 8/12/2019 Cs 1102 Tutorial 5

    12/32

    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)

  • 8/12/2019 Cs 1102 Tutorial 5

    13/32

    13

    First 15 minutes

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

    Postcondition : A[i..j] is sorted

    Find a pivot and position the elementsaccordingly

    QuickSort on left

    QuickSort on right

  • 8/12/2019 Cs 1102 Tutorial 5

    14/32

    14

    First 15 minutes

    Unsorted array

    quickSort : Sorts an imput array

    Find pivot

    pivot

    quickSort quickSort

  • 8/12/2019 Cs 1102 Tutorial 5

    15/32

    15

    First 15 minutes

    Unsorted array

    quickSort : Sorts an imput array

    Find pivot

    pivot

    quickSort quickSort

  • 8/12/2019 Cs 1102 Tutorial 5

    16/32

    16

    First 15 minutes

    QuickSort

    QuickSort(A[i..j])

    Precondition : Nil

    Postcondition : A[i..j] is sorted

  • 8/12/2019 Cs 1102 Tutorial 5

    17/32

    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

  • 8/12/2019 Cs 1102 Tutorial 5

    18/32

    18

    Question 1Solution

    (e)

    f(3,7)= f(2,-6)

    = f(1, 7)

    = f(0, -6)

    = -6

  • 8/12/2019 Cs 1102 Tutorial 5

    19/32

    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

  • 8/12/2019 Cs 1102 Tutorial 5

    20/32

    20

    Question 2

    Solution

    (d)

    g(n) where n is 0 terminates

    As g(0) = 1All other values do not terminate.

    g(-1) does not terminate, so g(1) also does not

    terminate

  • 8/12/2019 Cs 1102 Tutorial 5

    21/32

    21

    Question 3

    Consider the following function where the input n is

    assumed to be positive integer.

    f(n) = n2+ (n-1)2+ + 22+ 12

    Write a recursive function to implement it and a main()

    function to display the first 10 function values. What if n

    is not positive?

  • 8/12/2019 Cs 1102 Tutorial 5

    22/32

    22

    Question 3

    Solution

    int f(int n) {

    if (n

  • 8/12/2019 Cs 1102 Tutorial 5

    23/32

    23

    Question 4

    int h(int n) {

    if (n

  • 8/12/2019 Cs 1102 Tutorial 5

    24/32

    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 itis called with the same value and hence the recursivesolution is inefficient

  • 8/12/2019 Cs 1102 Tutorial 5

    25/32

    25

    Question 4

    Solution (1)

    To improve its performance, use iterative solution.

    int h(int n) {

    if (n

  • 8/12/2019 Cs 1102 Tutorial 5

    26/32

    26

    Question 4

    Solution (3)

    compute h(0), h(1), , until h(10) will be easier to get theanswer

    h(0) = 1

    h(1) = 1

    h(2) = 1h(3) = 1

    h(4) = 3

    h(5) = 5

    h(6) = 7

    h(7) = 11h(8) = 17

    h(9) = 25

    h(10) = 37

  • 8/12/2019 Cs 1102 Tutorial 5

    27/32

    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(n2)

    (d) O(2n)

    (e) O(n2n)

  • 8/12/2019 Cs 1102 Tutorial 5

    28/32

    28

    Question 5

    Solution

    (a)

    Domain of interest is reduced by half for each

    recursive call.

  • 8/12/2019 Cs 1102 Tutorial 5

    29/32

  • 8/12/2019 Cs 1102 Tutorial 5

    30/32

    30

    Question 6

    Solution

    a. Computing the sum of the first n even integersby 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 liste. Displaying one array element

    f. Displaying the last integer in a linked list

    g. Searching an array of n integers for aparticular value by using a binary search

    h. Adding an item to a stack of n itemsi. 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)

  • 8/12/2019 Cs 1102 Tutorial 5

    31/32

    31

    Question 7

    Consider the following method f, which calls the method swap.Assume that swap exists and simply swaps the contents of thetwo 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?

  • 8/12/2019 Cs 1102 Tutorial 5

    32/32

    32

    Question 7

    Solution

    O(n)

    as the statements in the while loop only execute once

    for each j because of the condition i