Lab 1: Sorting

(Laura Toma)


This lab serves as a Java warm-up and an introduction to good programming style, code modularity, and code testing. You will do all of these while solving a fundamental (albeit not terribly fun) problem: sorting. You will implement an algorithm for sorting called selection sort. You will use a main method, and you will write code to test the correctness of your sorting. Your will break up your sorting function into smaller function and thus practice (and appreciate) modularity.

The structure of your code

Your code should contain a single class, called SelectionSort. The data to be sorted will be its instance variable.
double [] data;
The class should have a constructor that allocates the array and initializes it with data.
public SelectionSort()
Use random values in the interval [0.0, 1000.0]. For how to create random values in Java search for, say, "Java random". You will find information about class Random. The class can generate random values in the interval[0.0, 1.0]. You will need to multiply the numbers appropriately to get values in [0.0, 1000.0]. Obviously, the value 1000.0 should be declared as final static in your class. The size of the array should also be declared as a constant.

It would be nice to add some prints in the constructor that inform the user that the array is being initialized, with what type of values, and what is its size. This is true in general, and its good to get used to making your code print its progress. It will help you tons in debugging.

To sort the data, you will write a method:

public void sort()
This method will implement selection sort. You can write the entire code for selection inside this function; however, I suggest that you write a helper function that will select the smallest element, and use it inside sort. I also suggest that you write a function that swaps two values in the array.
//finds the smallest element in the range from start to end and
//returns its position
int selectMin(int start, int end)

//swaps data[i] with data[j]
void swap(int i, int j)
Finally, to test your sort you will write a method that checks that data is sorted:
//returns true if data is sorted in <=  order
public bool isSorted()

The main method

Your class should contain a main method. This method first creates a SelectionSort object, then calls the sort method on it to sort it, and finally calls the isSorted method to see if it's sorted. Depending on the result returned by isSorted, it should print out to the user a message telling whether the sort was succesfull or not.


Unless your code works at the first try (that never happens), you will need to debug. The main thing that you'll learn as you become a more experienced programmer is how to debug, and, hopefully, how to write your code so that debugging is easier. Any experienced programmer structures and writes the code from the very beginning in such a way that debugging is easy.

For example, you may want to add a print method that prints the values in data[]. What I see people doing over and over again is that they write a loop to print the array every time they need it. Gee, this is so ugly and inconvenient. Just write a singel print method, and call it every time you want to see the array.

The most common method for debugging involves the use of print statements. The other thing you could do is learn how to use the BlueJ Debugger. You can find links on the class website. If you learn to use the Debugger effectively (and it isn't difficult) it will save you many hours of work during the semester.

What to hand in

  1. Email me the Java file as attachment;
  2. Hand in hard copy, and sign on the front page that you followed course rules.

Good luck!

Last modified: Wed Jan 26 10:53:56 EST 2011