RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

lect13b
selection sort

We will discuss how to sort an array of doubles. In particular, we'll use an algorithm called “selection sort” so that we can write it in-place.

Working through this by hand, we realize that a handy helper-function would be indexMin; we already wrote the essentially-similar method indexMax earlier.
As we step through our program, we realize that indexMin isn't acutally quite what we want; we don't want the (index of the) minimum of the entire array, but only from location i onward.

It is left to the reader to write indexMinFrom( int[] data, int startFrom ). (This is a good self-check; try writing the method on your own, and come by office hours if you want feedback on what you did. Writing indexMinFrom might make a fine exam problem!) Note that you will not need to come up with this sorting algorithm on your own, but you do need to be able to see the code and understand how it works.

Here is a version using while:

    void selectSort( double[] nums ) {
        int i = 0;
        while (i < nums.length) {
            int indexOfMin = indexMinFrom(nums,i);

            // Now, swap nums[i] with nums[indexOfMin]:
            int tmp = nums[i];
            nums[i] = nums[indexOfMin];
            nums[indexOfMin] = tmp;

            ++i;
        }
    }
Here is the same thing written using for:
    void selectSort( double[] nums ) {
        for ( int i=0;  i < nums.length;  ++i ) {
            int indexOfMin = indexMinFrom(nums,i);

            // Now, swap nums[i] with nums[indexOfMin]:
            int tmp = nums[i];
            nums[i] = nums[indexOfMin];
            nums[indexOfMin] = tmp;
        }
    }
One student pointed out that the code becomes even more understandable if we factor the swapping into its own method:
    void selectSort( double[] nums ) {
        for ( int i=0;  i < nums.length;  ++i ) {
            int indexOfMin = indexMinFrom(nums,i);
            swap( nums, i, indexOfMin );
        }
    }
(In fact, we could even dispense with our local variable indexOfMin, and have a one-line body if you really wanted: swap( nums, i, indexMinFrom(nums,i) ). It's arguable whether this is more understandable or not; once you are very comfortable with function-calls and return values, the one-line body does seem more natural.

a visualization. Note the code they have: what we did as indexMinFrom is in-lined as a nested loop. (Their variable k corresponds to the so-far variable inside indexMinFrom.)

Two-dimensional arrays

Not covered in class — Well, we just flashed up the picture and talked about it for 3min. We will cover it in lab tomorrow.

We can have arrays of any type, in Java. We've seen, say, array-of-double (double[]). But we might also have array-of-array-of-double (double[][]). We call this a two-dimensional array.

The book gives an example 2-D arrays to show a tic-tac-toe board in §7.6.

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Apr.23 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme