import java.util.*;
public class Project2 {
/** Task: Sorts equally spaced elements of an array into
* ascending order.
* @param a an array of Comparable objects
* @param first the integer index of the first array element to
* consider; first >= 0 and < a.length
* @param last the integer index of the last array element to
* consider; last >= first and < a.length
* @param space the difference between the indices of the
* elements to sort */
private static <T extends Comparable<? super T>> int incrementalInsertionSort(T[] a, int first, int last, int space) {
int unsorted, index;
int count = 0;
for (unsorted = first + space; unsorted <= last; unsorted = unsorted + space) {
T firstUnsorted = a[unsorted];
for (index = unsorted - space; (index >= first) && (firstUnsorted.compareTo(a[index]) < 0); index = index - space) {
a[index + space] = a[index];
count++;
} // end for
a[index + space] = firstUnsorted;
} // end for
return count;
} // end incrementalInsertionSort
public static <T extends Comparable<? super T>> int shellSort(T[] a, int first, int last) {
int count = 0;
int n = last - first + 1; // number of array elements
for (int space = n / 2; space > 0; space = space / 2) {
for (int begin = first; begin < first + space; begin++) {
count += incrementalInsertionSort(a, begin, last, space);
} // end for
} // end for
return count;
} // end shellSort
public static void main(String[] args) {
Random random = new Random();
Integer[] a = new Integer[10];
Integer[] b = new Integer[10];
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
b[i] = a[i];
System.out.println("Random");
}
System.out.println("Here");
Integer insertion = incrementalInsertionSort(a, 0, 19, 0);
System.out.println("The incrementalInsertionSort had " + insertion + " comparisons.");
Integer shell = shellSort(b, 0, 19);
System.out.println("The shellSort had " + shell + " comparisons.");
}
}
When I run this program, it compiles fine but it never finishes. I use eclipse and the method incrementalInsertionSort() has a red square in the outline instead of a green circle. This method and shellSort are copied directly out of a book. The main() method is what I did. Does anyone know what I can do/try that will make the program finish? Does anyone know what the red square in the outline section of eclipse means? Thanks.

New Topic/Question
Reply




MultiQuote





|