import java.util.Arrays; // File: Insert.java // A Java application to illustrate the use of an insertion sort // Additional javadoc documentation is available at: // http://www.cs.colorado.edu/~main/docs/Insert.html /****************************************************************************** * The Insert Java application illustrates an insertion sort. * *

Java Source Code for this class:
* * http://www.cs.colorado.edu/~main/applications/Insert.java * * * @author Michael Main * (main@colorado.edu) * * @version * Jun 12, 1998 ******************************************************************************/ public class Insert { /** * The main method illustrates the use of an insertion sort to sort a * small array. * The String arguments (args) are not used * in this implementation. **/ public static void main(String[ ] args) { final String BLANKS = " "; // A String of two blanks int i; // Array index int[ ] data = { 1000, 80, 10, 50, 70, 60, 90, 20, 30, 40, 0, -1000 }; int[] data1 = data.clone(); int[] data2 = {1000, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0, -1000}; // Print the array before sorting: System.out.println("Here is the entire original array:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); // Sort the numbers, and print the result with two blanks after each number. insertionsort(data, 0, data.length); // System.out.println("I have sorted all but the first and last numbers."); System.out.println("The numbers are now:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); System.out.println("==================================================="); System.out.println( ); System.out.println( ); System.out.println("Here is the entire original array:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); // Sort the numbers, and print the result with two blanks after each number. insertionsort(data); // System.out.println("I have sorted all but the first and last numbers."); System.out.println("The numbers are now:"); for (i = 0; i < data.length; i++) System.out.print(data[i] + BLANKS); System.out.println( ); System.out.println("==================================================="); System.out.println( ); System.out.println( ); System.out.println("Here is the entire original array:"); for (i = 0; i < data2.length; i++) System.out.print(data2[i] + BLANKS); System.out.println( ); // Sort the numbers, and print the result with two blanks after each number. insertionsort(data2); // System.out.println("I have sorted all but the first and last numbers."); System.out.println("The numbers are now:"); for (i = 0; i < data2.length; i++) System.out.print(data2[i] + BLANKS); System.out.println( ); } /** * Sort an array of integers from smallest to largest, using an insertion sort * algorithm. * @param data * the array to be sorted * @param first * the start index for the portion of the array that will be sorted * @param n * the number of elements to sort *
Precondition:
* data[first] through data[first+n-1] are valid * parts of the array. *
Postcondition:
* If n is zero or negative then no work is done. Otherwise, * the elements of data have been rearranged so that * data[first] <= data[first+1] <= ... <= data[first+n-1]. * @exception ArrayIndexOutOfBoundsException * Indicates that first+n-1 is an index beyond the end of the * array. * */ public static void insertionsort(int[ ] data, int first, int n) { int i, j; // Loop control variables int entry; // The element that is currently being inserted for (i = 1; i < n; i++) { entry = data[first+i]; for (j = first+i; (j>first) && (data[j-1] > entry); j--) { data[j] = data[j-1]; System.out.println(Arrays.toString(data)); } data[j] = entry; System.out.println(Arrays.toString(data)); } } //------------------------------------------- public static void insertionsort(int[ ] data) { int i, j; // Loop control variables int entry; // The element that is currently being inserted int n = data.length; for (i = 1; i < n; i++) { entry = data[i]; for (j = i; (j>0) && (data[j-1] > entry); j--) { data[j] = data[j-1]; System.out.println(Arrays.toString(data)); } data[j] = entry; System.out.println(Arrays.toString(data)); } } }