Week 02 Tutorial
Analysis of Algorithms

[Show with no answers]   [Show with all answers]

  1. (Counting primitive operations)

    The following algorithm

    Count the number of primitive operations (evaluating an expression, indexing into an array). What is the time complexity of this algorithm in big-O notation?

    for all i = n down to 1 do
        for all j = n down to i do
            print A[i] A[j]
        end for
    end for

    [show answer]

  2. (Algorithms and complexity)

    Develop an algorithm to determine if a character array of length n encodes a palindrome — that is, it reads the same forward and backward. For example, racecar is a palindrome.

    1. Write the algorithm in pseudocode.

    2. Analyse the time complexity of your algorithm.

    3. Implement your algorithm in C. Your program should accept a single command line argument, and check whether it is a palindrome. Examples of the program executing are

      ./palindrome racecar
      yes
      ./palindrome reviewer
      no
      

      Hint: You may use the standard library function strlen(3), which has prototype size_t strlen(char *), is defined in <string.h>, and which computes the length of a string (without counting its terminating '\0'-character).

    [show answer]