\( \newcommand{\blah}{blah-blah-blah} \newcommand{\eqb}[1]{\begin{eqnarray*}#1\end{eqnarray*}} \newcommand{\eqbn}[1]{\begin{eqnarray}#1\end{eqnarray}} \newcommand{\bb}[1]{\mathbf{#1}} \newcommand{\mat}[1]{\begin{bmatrix}#1\end{bmatrix}} \newcommand{\nchoose}[2]{\left(\begin{array}{c} #1 \\ #2 \end{array}\right)} \newcommand{\defn}{\stackrel{\vartriangle}{=}} \newcommand{\rvectwo}[2]{\left(\begin{array}{c} #1 \\ #2 \end{array}\right)} \newcommand{\rvecthree}[3]{\left(\begin{array}{r} #1 \\ #2\\ #3\end{array}\right)} \newcommand{\rvecdots}[3]{\left(\begin{array}{r} #1 \\ #2\\ \vdots\\ #3\end{array}\right)} \newcommand{\vectwo}[2]{\left[\begin{array}{r} #1\\#2\end{array}\right]} \newcommand{\vecthree}[3]{\left[\begin{array}{r} #1 \\ #2\\ #3\end{array}\right]} \newcommand{\vecfour}[4]{\left[\begin{array}{r} #1 \\ #2\\ #3\\ #4\end{array}\right]} \newcommand{\vecdots}[3]{\left[\begin{array}{r} #1 \\ #2\\ \vdots\\ #3\end{array}\right]} \newcommand{\eql}{\;\; = \;\;} \definecolor{dkblue}{RGB}{0,0,120} \definecolor{dkred}{RGB}{120,0,0} \definecolor{dkgreen}{RGB}{0,120,0} \newcommand{\bigsp}{\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;} \newcommand{\plss}{\;\;+\;\;} \newcommand{\miss}{\;\;-\;\;} \newcommand{\implies}{\Rightarrow\;\;\;\;\;\;\;\;\;\;\;\;} \)


Homework 1


 

Note: Credit will not be given just for answers - show all your work: your reasoning, steps you took to get your answer etc.

  1. Read through an example of how to go from a high-level idea to an actual implementation.

  2. Pen-and-paper (submit as PDF) exercises:
    1. Find the appropriate "order" relationship between \(n \log(n)\) and \(10n\) and find the constants \(c\) and \(N\).

    2. Among the following functions, for each distinct pair \(f_i(n)\) and \(f_j(n)\) identify which of the possible "order" relationships are satisfied: \(f_i(n) = O(f_j(n))\), \(f_i(n) = \Omega(f_j(n))\), or \(f_i(n) = \Theta(f_j(n))\). The functions are (use base-2 for logarithms). $$\eqb{ f_1(n) & \eql & n \\ f_2(n) & \eql & n \log(n) \\ f_3(n) & \eql & n \log(n^2) \\ f_4(n) & \eql & n^{2.5} \\ f_5(n) & \eql & n \sqrt{n} \\ f_6(n) & \eql & n^2 \log(n) \\ }$$ You do not need to find \(c\) and \(N\) in every case (although you certainly can), but you do need to provide reasoning (a few sentences). For example, in comparing \(f_1(n)\) and \(f_2(n)\) you could say:
      • \(\log(n)\) is an increasing function, and \(\log(n) \gt 1\) for \(n \gt 2\). Therefore \(n \log(n) \gt n\) for \(n \gt 2\). This means, at the very least, \(f_1(n) = O(f_2(n))\) and \(f_2(n) = \Omega(f_1(n))\). But because the gap between \(n \log(n)\) and \(n\) grows with \(n\), we can't find a constant \(c\) such that \(n\log(n)\lt c n\). This means, \(n\log(n) \neq O(n)\) and therefore \(f_1(n) \neq \Theta(f_2(n))\). Summary: \(f_1(n) = O(f_2(n))\) and \(f_2(n) = \Omega(f_1(n))\)

    3. Analyze the following code and provide a "Big-Oh" estimate of its running time. Explain your analysis.
      
          for (int i=0; i < n; i++) {
            for (int j=0; j < n*n*n; j++) {
              sum += data[i] * j;
            }
            int k = 4*n*(n-1);
            while (k > 0) {
              sum += data[k % n];
              k--;
            }
          }
        
    4. Consider an M×N matrix (or 2D array), with M rows, and N columns. Now consider the four quadrants one gets by bisecting the matrix vertically and horizontally. The four quadrants are of equal size. Write pseudocode to determine if all four quadrants are identical, making sure to account for the cases when M and N are even or odd. In the case that M or N is odd, you'll have to ignore the middle row (or column), as the case may be. For example, the matrix on the left has four quadrants equal (with one of those quadrants shown in bold), while the one on the right fails the test:
      
              6 5 3 6 5              3 4 2 3 4 2
              7 7 3 7 7              1 2 2 1 2 2
              8 2 9 8 2              3 4 2 3 4 2
              6 5 1 6 5              1 2 2 1 1 2
              7 7 1 7 7
              8 2 0 8 2
         
      Here, because the number of columns is odd, we ignore the middle column for symmetry. In Big-Oh notation, how long does your algorithm take to execute when M=N?

  3. For the rest of the semester, it will be essential to know how to work with Java interfaces, both with interfaces created expressly for this course and the Java API's own interfaces.
    • Review Java interfaces.
    • Especially examine the Comparable interface. Try to find some examples using that interface.
    • Write the few lines of code needed to make the Comparable interface work for the object A in this example: CompInterfaceExample.java.
    • In your PDF for this exercise, draw a memory diagram showing the contents of the system stack (method stack) and heap before the first call to compareTo() returns. How does Java's sort method know how to sort the array of A instances, since the sort method was written and compiled long before the code for class A was written?
    Note: You will almost never write the code for an interface. Instead, your classes will implement interfaces that are already in algtest. For example, in the code below, your sorting algorithm will need to implement the Algorithm interface (among other interfaces). You have the link to the description of the interface (which tells you what methods your class needs to implement), but you don't write the interface code itself. That code is already in algtest.jar.

  4. Implement the Insertion Sort method (see the Wikipedia entry) and compare it to the SelectionSort technique in Module 1. To implement, you will need to follow these steps:
    • First: (1) create a separate directory for the course called cs3212; (2) create a directory called submit off of your home directory.
    • Follow the submission instructions to download the algorithm test environment.
    • Create a subdirectory called hw1 You will implement AND execute your insertion sort code in this directory.
    • Next, download the following two text files: hw1.props and hw1.props2.
    • Write your code in files called InsertionSort.java and SelectionSort.java.
    • Both of these need to implement the SortingAlgorithm interface. Note that you will also need to implement the Algorithm interface because SortingAlgorithm extends Algorithm.
    • NOTE: both interfaces Algorithm and SortingAlgorithm are included in algtest.jar. You do not write the code for these interfaces.
    • When your class implements the Algorithm interface, only provide an empty implementation for setPropertyExtractor.
    • You will only need to write code for the sortInPlace (int[]) method in the SortingAlgorithm interface. However, you will need to provide at least empty implementations (method definition, but empty body - just the curly brackets) for the other methods. We will not test the other methods.
    • Once you have written the code for both algorithms and want to test: get into the hw1 directory and simply type the following (for Unix) at the commandline:
      
            java -jar ../algtest.jar hw1.props
          
      (You can run something similar with a DOS-prompt on Windows). The jar file contains the algorithm test environment which will run your program and perform some tests.
    • If you like you may run a test with the second properties file, hw1.props2.

  5. In this part of the exercise, you will write code that takes as input an array of integers and rearranges the array to have the following property:
    • Suppose the first element in the original array has the value x.
    • In the new array, suppose that x is in position i, that is data[i] = x. Then, we want the rearranged array to be such that data[j] <= x for all j < i and data[j] > x for all j > i. Thus, informally, the rearranging places all the values to the "left" of x are less than (or equal to) x and all values to the right are larger than x.
    • Here's an example. Suppose the array has the elements in this initial order:
      
             4 3 9 2 7 6 5
          
      After one possible (there are many) partition, we get:
      
             3 2 4 5 9 7 6
          
      That is, the leftmost element, 4, is positioned in the resulting array so that all elements less than 4 (that is, 2, 3) are to its left (in no particular order), and all elements larger than 4 are to its right (again, in no particular order).
    • This same idea, which we will call partitioning, can be applied to a part of an array as well. For example, suppose we partition the part of the array below in bold:
      
             4 3 6 2 9 7 5
          
      After partitioning that part, we will get something like:
      
             4 3 2 6 9 7 5
          
      (using 6 as the partitioning element).
    Write your code in a file called MyPartition.java:
    • Make your class it implement the PartitionAlgorithm interface.
    • You will only need to write code for the leftIncreasingPartition method. Provide an empty implementation for the other - we will not test it. Note: your partitioning code will be in this method.
    • Observe that you are given a sub-range within the array which you will re-arrange. Do NOT touch elements outside the subrange.
    • Use the hw1.props3 properties file for this problem.

Note:

Submission: