Lab 10: Selection Sort in Java

Objectives

In this lab you will write a simple Java program that implements the selection sort algorithm. The goal is to gain experience with Java syntax and explore the similarities and differences between programming in Python and Java.

Before you start this lab, review the selection sort algorithm from the lecture on sorting.

Getting Started

Clone the lab resources from the gitlab repository, as usual:

cd cs134
git clone https://evolene.cs.williams.edu/cs134-labs/22xyz3/lab10.git

where your CS username replaces 22xyz3. If you are working with a partner, replace this with your CS username and your partner’s CS username, sorted, and separated by a - (eg, 22xyz3-24abc2). The description of the files provided in the starter is available in README.md and discussed below.

We have provided you with a Python script selectionSort.py which contains an implementation of the function selectionSort along with the if __name__ == "__main__" suite which calls the sorting algorithm on a randomly generated list. We have also provided you SelectionSort.java with some skeleton code for getting started. This is the file you will be typing in for this lab.

We encourage you to use the lab computers for this assignment. However, if you would like to complete this lab on your laptop, you will need to install Java. Follow the instructions found here for your computer.

Completing SelectionSort.java

Start by reviewing the Python code provided in selectionSort.py. You will be translating this code pretty much line-by-line to Java. We have provided comments to help guide you in this process.

Your only task for this week is to complete the implementation of the selectionSort method in SelectionSort.java, along with the main method which creates an random array of numbers, calls the selection sort method, and prints the array before and after sorting. Look for the commented TODO lines in the code for more guidance. Note that your code should produce output that matches the output produced by the Python script with respect to formatting. You may find the static method Arrays.toString(array) useful for printing the contents of your array.

Compiling and Executing your Code

To test your code, remember that you first need to compile it before you execute it.

javac SelectionSort.java

If you have syntax errors in your code (missing types, semicolons, parentheses, curly braces, etc), this step will call attention to those errors.

Once the compiler runs smoothly, notice that it creates the SelectionSort.class file, by typing ls in your Terminal.

ls

Finally, when you are ready to execute your code, you can do so by typing java followed by name of file (without the .class extension):

java SelectionSort

Submitting Your Work

When you’re finished, stage, commit, and push your work to the server as in previous labs.

  • Do not change the method headers provided in the starter code.

  • Make sure you implement the TODOs listed in starter code

Grading Guidelines

See lab Syllabus for an overview of the rubric used in grading. The file Gradesheet.txt gives you specifics of our expectations for this lab.

Functionality and programming style are important, just as both the content and the writing style are important when writing an essay. Make sure your variables are named well, and your use of comments, white space, and line breaks promote readability. While indentation is not required in Java, it is still considered good style to use it as you did in Python.