The goal of this project is to implement basic sorting algorithms. This will review and reinforce your understanding of how to manipulate arrays as well as give you practice in the basic syntax of writing methods.
Beginning with this project, documentation and style will be assessed as part of your grade. Make sure you conform to conventions for identifiers and properly document all parts of your program.
The handout on sorting describes what computer scientists mean when we talk about sorting data. That handout also describes three algorithms for sorting arrays of integers.
Move into the directory where you put files for this class, and then clone the repository to get the starting code:
$ hg clone /cslab/class/csci235/projects/project3
Now you can cd
into the project3
.
You should find four Java source files there. Sort.java
provides the main method for this assignment; it includes some parts
that you don't need to understand, but it allows you to exercise any
.java
file that provides a suitable method
sort()
.
The other three Java files will each provide a sort()
method. Bubble.java
is already filled in, with the
method that we wrote in class on Monday. To run it, you need to
compile both Sort.java
and Bubble.java
, then
run
$ java Sort Bubble
This will create an array filled with random values and display it.
It then calls the sort method in Bubble
and prints the
array again after that. If we got it right in class, we should see
that the second time it prints in ascending order.
Take some time to read the bubble sort method and compare it to the description from the handout.
Your task is to complete the other two sorting methods to use the appropriate algorithms.
Consult the handout for a description of selection sort.
Open the file Selection.java
in Emacs.
You'll see that it contains an empty method sort()
.
You will need to fill in the method.
You can compile and run the program now.
$ javac Selection.java $ java Sort Selection
The program won't crash, but it won't sort the array either.
Remember that anytime you make a change to Selection.java
,
that's the file you need to recompile, but
you do not need to recompile Sort.java
(because
you're not going to make any changes to it).
Follow these steps to write a selection sort program:
Compile and test. When you're confident it is working correctly, commit it to your repository before you move on.
To set up this part, open the file Insertion.java
.
See the handout for a description of
insertion sort, but you'll need to figure out how to proceed.
This algorithm will also involve a loop within a loop.
Complete the method.
If you want to test just part of it, you can write your own
main()
method at the end of Insertion.java
.
If you do that, you might find it helpful to call one or both of the
methods provided by Sort.java
to randomly fill an array
or to print it out. For that you'd run
$ java Insertion
When you have it complete, test it by running it using
Sort
as the driver:
$ java Sort Insertion
Create a script file that shows you compiling
Sort.java
plus the two files that you have written. Then
run each sort a couple of times.
Hand in your source files and script electronically as project3
.
DUE: Friday, Feb. 19, at 5:00 PM.