Homework Assignments for CS 332

Some of your homework assignments will be programming assignments and for other homework assignments you will turn in written solutions to problems. The programs that you turn in will be graded according to the criteria given in the first of the following two links. The second link gives detailed instructions on exactly how you are to turn in your finished assignments.

Program grading criteria
Turning in your assignments

Below are your homework assignments. The due date for each one is contained in the assignment description.

Assignment 7.
Do the problems contained in this PDF document. This assignment is due Friday, May 6.

Assignment 6.
The details of this assignment are in this page. This assignment is due on Monday, April 25.

Assignment 5.
Do the problems contained in this PDF document. This assignment is due Thursday, March 24.

Assignment 4.
Do the following four exercises from page 226 of the book by Sedgewick, Exercises 5.44, 5.45, 5.46, and 5.47. This assignment is due Tuesday, March 8.

Assignment 3.
Do the following four programming exercises from page 218 of the book by Sedgewick, Exercises 5.25, 5.28, 5.30, and 5.36. Below are some notes about each exercise. This assignment is due Thursday, February 24.

Exercise 5.25: Test your recursive method with a main() method that gets the needed parameter n from the command line. Have the main() method print out the array.

Exercise 5.28: Do this both iteratively and recursively. Test your methods with a main() method that draws vertical "ascii rulers" like the following one.

0

00

0

000

0

00

0
Exercise 5.30: See the following URL for a list of basic PostScript functions or see this page for a reference on all of the PostScript functions. And read this introduction to the PostScript language. Write your program to send its PostScript commands to standard output (i.e., use System.out.print()). Then you can use the command line to either redirect standard output to some file or pipe the output of your program directly into gswin32c.exe, the PostScript interpreter for Windows (actually, you want to create a Textpad tool to do this for you).

Exercise 5.36. Take the "integer coordinate space" in this problem to be a Java graphics context. This problem is asking you to implement a (recursive) "line drawing algorithm". That is, use a graphics primitive that can only plot individual points to draw a line segment. Write a recursive function
static void drawSimpleLine(Graphics g, int x1, int y1, int x2, int y2)
that draws a line on the Graphics context g by only plotting points. Plot a single point by using the drawLine() method from the Graphics class this way: drawLine(x, y, x, y).

Assignment 2.
Write a Java class file that contains static methods that implement the following sorting algorithms:
  1. iterative selection sort,
  2. recursive selection sort,
  3. iterative insertion sort,
  4. recursive insertion sort (see CLR, Exercise 1.3-4, page 15).
  5. merge sort (you should write two methods, a recursive mergeSort() and a merge(), as described in CLR, pages 12-13).
Write a main() method that runs your five sorting algorithms on random arrays with a range of sizes (use one of the methods from the last assignment to create randomly permuted arrays). Your program should gather statistics on how long each algorithm runs for each array size. Your program should output its statistics in a reasonably formatted manner. Cut and paste your program's output into a text file and give an explanation of why your data shows that selection and insertion sort are O(n^2) algorithms and merge sort is an O(n*ln(n)) algorithm.

The size of your arrays can be powers of 2 (which makes things easier for merge sort). Your data for merge sort should go up to at least an array size of 4,194,304 integers.

In addition, gather statistics on the running time of your algorithms on sorted arrays. Your data should show that selection sort is still O(n^2) and merge sort is still O(n*ln(n)) for arrays that are already sorted, but insertion sort is O(n) for sorted arrays. How do the running times for selection sort and merge sort compare for sorted vs. random arrays? Do either of these two algorithms show any speed up for sorted arrays? Explain why or why not. Your statistics on sorted arrays should be displayed and explained separately from the statistics on random arrays.

To get data on how long a sorting algorithm runs on a particular array, use the Java method System.currentTimeMillis(). You time the execution of a method call like this:

   int time0 = System.currentTimeMillis();
   sort(myArray,0,n);  // function to be timed
   int executionTime = System.currentTimeMillis() - time0;

This is due Thursday, February 10.

Assignment 1.
Download the following four Java programs.
Permutation1.java
Permutation2.java
Permutation3.java
Permutation4.java
The four programs all do exactly the same thing, i.e. they solve the same problem, but they do it in completely different ways, i.e., they each use a different algorithm. The programs each define a (static) method permutation() that fills an array of size n with a random permutation of the integers from 1 to n (that is, a random reordering of all the integers from 1 to n). Each program has a different implementation of permutation().

Your assignment is to analyze the four implementations of permutation() and document your analysis in each of the four Java programs. Which of the four algorithms is the fastest? Which is the slowest? What makes the fastest one faster than the others and what makes the slowest one slow? How much faster is the fastest one than the slowest one? For each algorithm, how long it runs depends on the size of the permutation being computed; how does each algorithm's execution time depend on the size of the permutation? For what size permutation does each program become impractical to run? Your answers should be specific. Try to really figure out what is going on in each implementation of permutation().

You will turn in to me the four Java programs with your analysis of each one contained in comments in the code. Put the four files in a zip file and email me the zip file as per the homework instructions.

This is due Tuesday, February 1. After you have all turned in your explanations we will discuss this problem in class and go over the various explanations.


Return to the CS 332 home page.


compliments and criticisms