Assignment

Contents:

  • Overview
  • Internet Requirements
  • Practice Problems
  • Problems to be Submitted
  • Extra Credit

  • Overview

    Topic: Algorithms
    Related Reading: Ch. 6.1, 9.4, 9.5 and pp. 518-525
    Due:

    Internet Requirements

    You may want an Internet connection for several practice problems. However you do not necessarily need the connection for completing the assignment, other than for submission.

    Practice Problems

  • Exercise 7 of Ch. 6 (p. 182); answer in back of text

  • Exercise 13 of Ch. 6 (p. 182); answer in back of text

  • Exercise 11 of Ch. 9 (p. 314); answer in back of text

  • Exercise 14 of Ch. 9 (p. 315); answer in back of text

  • Exercise 16 of Ch. 9 (p. 315); answer in back of text

  • Consider the following sorted list of numbers:

    2 4 7 10 13 16 17 20 24 28 31 38 41 43 45

  • What sequence of 'middle' values are compared to the target when performing binary search with target 28?

  • What sequence of 'middle' values are compared to the target when performing binary search with target 16?

  • What sequence of 'middle' values are compared to the target when performing binary search with target 5?
  • Note: The solution for the above questions can be found by running a Binary Search Demonstration with parameter "#items: 15" and "seed: 15849" and with target set appropriately.


  • Problems to be Submitted (20 points)

    1. (4 points)
      Exercise 12 of Ch. 9 (p. 314)
      [Note: please ignore the value "length" which is shown as 8. I do not know why they say 8 as opposed to 11, since there are 11 items.]

    2. (5 points)
      Exercise 13 of Ch. 9 (p. 314)
      Though it does not state this explicitly, please answer this question in reference to the Quicksort algorithm.
      Also, please note that you only need to simulate the algorithm until just before the first recursive call is made (namely, before the statement "Quicksort the left half" shown on p. 292. That is, I want you to do the partitioning (described at bottom of p. 293) but then to stop and show me the state of affairs.

    3. (5 points)
      Consider the following sorted list of numbers:

      2 4 9 16 19 21 25 27 32 33 35 38 41 42 43

      1. What sequence of 'middle' values are compared to the target when performing binary search with target 4?

      2. What sequence of 'middle' values are compared to the target when performing binary search with target 17?

      3. What sequence of 'middle' values are compared to the target when performing binary search with target 22?

      4. What sequence of 'middle' values are compared to the target when performing binary search with target 34?

      5. What sequence of 'middle' values are compared to the target when performing binary search with target 41?

    4. (3 points)
      What value or sequence of values will be printed when running the following recursive routine, called originally as TestA(1):
        TestA(Count)
           if (Count<5) then
              print value of "Count"
              call TestA(Count+1)
           endif
      

    5. (3 points)
      What value or sequence of values will be printed when running the following recursive routine, called originally as TestB(1):
        TestB(Count)
           if (Count<5) then
              call TestB(Count+1)
              print value of "Count"
           endif
      
    Overall, please type your answers to all of the problems in a single document to be submitted electronically. Please see details about the submission process.

    Extra Credit (2 points)

    The goal here is to really challenge your understanding of recursive procedures. A classic problem, known as the Towers of Hanoi can be solved with the following recursive procedure:
    Towers(n,A,B,C)
      if (n>0) then
        Call Towers(n-1,A,C,B)
        Print "Move ring <n> from <A> to <C>"
        Call Towers(n-1,B,A,C)
    

    To prove your understanding, please describe the precise output which is generated when a call is made as Towers(4,X,Y,Z).


    Last modified: 12 November 2002