Gettysburg College

CS 111
Computer Science I

Fall 2025

Assignment 7

Due: Thu, Oct 23 by 11:59pm

General description

The goal of this assignment is to implement, analyze, and compare the three sorting algorithms discussed in class.

The focus of this assignment is on for loops, array processing, and converting a non-trivial algorithm from its description to Java code.

Preliminaries

Start DrJava and create a new project named SortAlgosApp in folder cs111/hw . See the Readings List notes on how to create a new project.

All arrays in this project should be double[].

Copy the following methods which will be used for testing:

double[] toArray( double... numbers )
{
    return numbers;
}
Later in public void run():
double[] pickArrayName;

pickArrayName = toArray( n, u, m, b, e, r, s );
test a method
printArray( if necessary )

pickArrayName = toArray( n, u, m, b, e, r, s );
test a method
printArray( if necessary )
Always keep first line. Rest can be commented out.
void printArray( double[] numbers )
{
    for ( int i = 0 ; i < numbers.length ; i = i + 1 )
    {
        System.out.print( numbers[ i ] + " " );
    }
    System.out.println();
}

With method toArray you can use the same variable name for the test array (see examples below), which should simplify testing (at least one time need to put double[] for the array variable).

Preliminaries

Pick the algorithm that you think is the easiest. Bubble Sort may conceptually be simpler, but some details can be tricky. Selection Sort might be the most straightforward.

Selection Sort

Recall the Selection Sort algorithm from class:

Selection Sort Outline

1. for each index/item in the array:

       2. find index of smallest element to the right of current element

       3. swap current and smallest, if out of order

Write the following methods:

Bubble Sort

Recall the Bubble Sort algorithm from class:

Bubble Sort Outline

1. for n-1 times:

       2. go through array and swap every pair that is out of order

Implement a modified version of the Bubble Sort algorithm that sorts an array of doubles in increasing order and incorporates some of the improvements discussed in class. Specifically: Write the following methods:

Insertion Sort

Recall the Insertion Sort algorithm from class:

Insertion Sort Outline

1. for each index/item in the array:

       2. push current item to the left (to the beginning of array)

Write the following methods:

Testing

The run() method must demonstrate thorough testing:

For all methods:

For Sort methods only:

The empty array and one-element array do not require anything special:

Gathering Execution Data

At the very top of your program put the following import statements (needed for the method given below):
import java.util.Random;
import java.util.Date;

Copy the following method. It generates different array configurations depending on the character parameter order:

    // returns an integer array of n elements such that
    // * if order is 'i' the array elements have values 1,2,...n
    // * if order is 'd' the array elements have values n,n-1,n-2,...,1
    // * if order is 'r' the array elements have random values
    //
    double[] generateArray( int n, char order )
    {
        Random rand = new Random( n );
        double[] numbers = new double[ n ];
        for ( int i = 0 ; i < n ; i = i + 1 )
        {
            if ( order == 'i' )
            {
                numbers[ i ] = i + 1;
            }
            else if ( order == 'd' )
            {
                numbers[ i ] = n - i;
            }
            else if ( order == 'r' )
            {
                numbers[ i ] = rand.nextInt();
            }
        }
        return numbers;
    }

Write the following method:

Comparison and Analysis: Data Collection

Do this part when you are done and are ready to collect the time measurements that will be plotted in Excel.

  1. Add -Xint option:

    • click Preferences and follow instructions in this image:

  2. rename run() to runIgnore()

  3. Copy the following method:
    public static void main( String[] args )
    {
        System.out.println( "Hello from regular Java" );
    
        SortAlgosApp app = new SortAlgosApp();
    
        app.testSortAlgorithms( 1000, 15000, 1000, 'i' );  // collect data for best case
        System.out.println();
    
        app.testSortAlgorithms( 1000, 15000, 1000, 'd' );  // collect data for worst case
        System.out.println();
    
        app.testSortAlgorithms( 1000, 15000, 1000, 'r' );  // collect data for average case
        System.out.println();
    }
    

Comparison and Analysis: Data Visualization

Using Excel create a single document with three sheets for each input configuration: sorted, reverse sorted, and random order input array.

  1. Save the document in a single file called SortAlgosApp.xlsx in the folder cs111/hw/SortAlgosApp/src/cs1/SortAlgosApp/. Rename the three sheets inside as Sorted, Reverse, and Random .

  2. Each plot should show the relationship between input size (x-axis) and run-time (y-axis) for the three sorting algorithms on the respective input configuration. Explanation on how to create XY-Scatter Chart is available here (for Excel).

  3. Fit a curve/trendline through the data for each algorithm for each plot. Use your judgment as to what curve is the best fit. Excel will allow you to fit a straight line, any polynomial curve (i.e. n?), exponential curve, etc. Our analysis from class suggested a polynomial trendline of either degree 1 (linear) or degree 2 (quadratic).

  4. Show the equation for each curve/trendline. Explanation on how to fit a trendline through scatter data is available here (for Excel).

  5. In the Sorted and Reverse sheets write what you expected to see based on the discussion from class and state whether your data reflects our analysis. (You do not have to write anything in the Random sheet).


What to turn in

Turn in the Java code for the app in the Assignment 7 dropbox: