Summary: This laboratory exercise introduces and analyzes the insertion sort as a mechanism to order numbers in an array.
Many applications require the maintenance of ordered data. In Java (and Scheme), a simple way to structure ordered data is in an array (or vector), such as the following ordered array A of integers:
One common sorting approach is based on code that assumes that the first part of an array is ordered and then adds successive items to this array segment until the entire array is sorted. To understand this approach, we first consider how to add one item to an ordered array segment. We then apply this work to each array element in turn to yield an ordered array.
Suppose items A[0], ..., A[k-1] are ordered in array A:
The following code inserts an item into the array, so that items A[0], ..., A[k] become ordered:
int i = k-1;
while ((i >= 0) && a[i] > item){
a[i+1] = a[i];
i--;
}
a[i+1] = item;
Using this basic insertion step, an array A can be sorted iteratively according to the following outline:
This outline gives rise the the following code, called an insertion sort.
public static void insertionSort (int [] a) {
// method to sort using the insertion sort
for (int k = 1; k < a.length; k++) {
int item = a[k];
int i = k-1;
while ((i >= 0) && a[i] > item){
a[i+1] = a[i];
i--;
}
a[i+1] = item;
}
}
insertionSort, so that the array is sorted in descending
order rather than ascending order. (After testing your revised program,
change the code back to its original form for use in the rest of this lab.)
The Java programming language provides a mechanism to compute run times of various programs. Specifically, System.currentTimeMillis() returns the amount of time (in milliseconds) since January 1, 1970. This method allows us to use the computer to time various algorithms.
long start_time;
long end_time;
(Here, long allows these variables to store larger integers than
the typical int integer data type.) Next, place the following
lines just before and after the call to insertionSort in the body
of main.
start_time = System.currentTimeMillis();
// the call insertionSort (c); goes here
end_time = System.currentTimeMillis();
Finally, place the following print statements after the printing of the
c array.
out.println ("start: " + start_time);
out.println ("end: " + end_time);
out.println ("elapsed: " + (end_time - start_time));
Now, compile and run the program, and explain what happens.
In your test, the elapsed time likely was shown as 0, since the time to sort a dozen items is smaller than a millisecond -- the accuracy of the clock. To get measurements that better illustrate efficiency issues, we must ask the machine to sort a larger data set. Rather than typing many data elements into the program during execution, we could generate test data as follows:
(In the last case, Math.random() generates a random real number between 0 and 1, so Math.random()*10000 generates a random real number between 0 and 10000. The "cast" or phrase (int) converts this to a random integer.)
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/152.sp01/lab-n-squared-sorts.html