CSC 120

Week 6


Sample Programs:

Here are some of the sample programs, and more, we looked at in class: Array.java Array2.java Duplicate.java Array4.java ArraySum.java ArraySum1.java ArraySum2.java ArrayTest.java TestCopy.java
SelectionSort.java
Reverse.java
TimedComputation.java

Reading Assignment:

Read Chapter 5
Here is a link to sorting algorithms: Sorting Algorithms

Please read the details of the Selection Sort algorithm: Selection Sort Selection Sort Analysis

Lab/Homework Assignment:


  1. Take the SelectionSort.java code (see the link above) and modify it so that it can sort an array of random integers of a given length. Then imbed your modification into the code fragment below to time how long it takes to sort 100, 1000, 10000, (100000 ??) random integers. Report your findings.
    public class TimedComputation {
            
       /* This program performs some mathematical computations and displays
          the results.  It then reports the number of seconds that the 
          computer spent on this task.
       */
    
       public static void main(String[] args) {
    
         long startTime;   // Starting time of program, in milliseconds.
         long endTime;     // Time when computations are done, in milliseconds.
         double time;      // Time difference, in seconds.
         
         startTime = System.currentTimeMillis();
         
        // :::::::::
        // PUT YOUR SORTING CODE HERE
        // :::::::::
         
         endTime = System.currentTimeMillis();
         time = (endTime - startTime) / 1000.0;
         
         System.out.print("\nRun time in seconds was:  ");
         System.out.println(time);
    
       } // end main()
    } // end class TimedComputation
    
    
    

  2. Hand simulate the sorting of the array {-7, 5, 6, 7, 1, 0, -77, 2} using the Selection Sort. That is, write the array after each pass. How many comparisons, and how many copy operations are used?

  3. Complete the code in Reverse.java Currently it just reads in an integer array and prints the array an its copy to the console. Add a new method called reverseArray() that prints the array in reverse order. The output should look like:
    Enter size of data[]:
    3
    Enter 3 integers:
    1 2 3
    Your Data
    1 2 3
    
    Copy Data
    1 2 3
    
    Reverse Data
    3 2 1
    

  4. Insert comments in your Reverse.java code for javadoc. Imitate the TwentyOnePickup.java code. There are many tags in javadoc but just use @param and @return. Remember to test your comments using the command
    javadoc -package Reverse.java
    You should do this in a subdirectory since many files will be created. You can view your documentation by looking at the index.html file using a browser.