import java.applet.*; import java.io.*; import java.util.*; public class lab9answers extends Applet{ public void start(){ // These are just sample lists for testing the code int[] a = { 9, -1, 3, 6, -10}; int[] b = {}; int[] c = { 8,3,-21,1000,3112,12,-5}; int[] d = { 1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70}; // Print out a test of array a IntArray.printArray(a); System.out.println("The sum of the elements of a is: "+ IntArray.sum(a)); IntArray.insertionSort(a); System.out.println("Sorted a "); IntArray.printArray(a); System.out.println("--------"); // Print out a test of array b IntArray.printArray(b); System.out.println("The sum of the elements of b is: "+ IntArray.sum(b)); IntArray.insertionSort(b); System.out.println("Sorted b "); IntArray.printArray(b); System.out.println("--------"); // Print out a test of array c IntArray.printArray(c); System.out.println("The sum of the elements of c is: "+ IntArray.sum(c)); IntArray.insertionSort(c); System.out.println("Sorted c "); IntArray.printArray(c); System.out.println("--------"); // Print out a test of array d IntArray.printArray(d); System.out.println("The sum of the elements of d is: "+ IntArray.sum(d)); IntArray.insertionSort(d); System.out.println("Sorted d "); IntArray.printArray(d); System.out.println("--------"); // Test of Vector v1 Vector v1 = new Vector(); v1.addElement(new Integer(10)); v1.addElement(new Integer(8)); v1.addElement(new Integer(6)); v1.addElement(new Integer(4)); v1.addElement(new Integer(2)); IntVector.printVector(v1); IntVector.reverseVec(v1); System.out.println("Reversed v1: "); IntVector.printVector(v1); // Dynamically change the contents of v1 v1.addElement(new Integer(0)); v1.addElement(new Integer(-25)); IntVector.printVector(v1); IntVector.reverseVec(v1); System.out.println("Reversed v1: "); IntVector.printVector(v1); } } // Methods that vectors find useful class IntVector { public static void reverseVec (Vector v) { int lo = 0; int hi = v.size()-1; while (lo < hi) { // Save the integer value of the vector v at the index lo in the state variable temp int temp = ((Integer)v.elementAt(lo)).intValue(); // Set the vector at index lo to be the object at the index hi v.setElementAt(v.elementAt(hi),lo); // Create a new Integer Object containing the integer number temp at the index hi v.setElementAt(new Integer(temp),hi); // Update the values of lo and hi lo=lo+1; hi=hi-1; } } // Prints out the array public static void printVector(Vector v) { //System.out.println("Don\'t you just love lab?"); System.out.print("Vector ["); for(int i = 0; i < v.size(); i++){ System.out.print(" "+((Integer)v.elementAt(i)).intValue()+" "); } System.out.println("]"); } } // Methods that arrays find useful class IntArray { // A class serving as a repository for methods that manipulate integer arrays. public static void reverse (int[] a) { int lo = 0; int hi = a.length-1; while (lo < hi) { // Swap the array contents of the values at indices lo and hi int temp = a[lo]; a[lo]=a[hi]; a[hi]=temp; // Update lo and hi lo= lo+1; hi=hi-1; } } // Prints out the array public static void printArray(int [] a) { //System.out.println("Don\'t you just love lab?"); System.out.print("Array : ["); for(int i = 0; i < a.length; i++){ System.out.print(" "+a[i]+" "); } System.out.println("]"); } public static int sum (int [ ] a) { // Return the sum of all elements in array a. int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i]; } return sum; } public static void dbl (int [ ] a) { // Double all of the elements in array a. for (int i = 0; i < a.length; i++) { a[i] = 2 * a[i]; } } public static int min (int [ ] a) { // Return the minimum integer in the array, or Integer.MAX_VALUE if it is empty. int minVal = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { minVal = Math.min(minVal, a[i]); } return minVal; } public static int minBetween (int [ ] a, int lo, int hi) { // Return the minimum integer in a[lo, hi], or Integer.MAX_VALUE segment is empty. int minVal = Integer.MAX_VALUE; for (int i = lo; i <= hi; i++) { minVal = Math.min(minVal, a[i]); } return minVal; } public static int minIndex (int [ ] a) { // Return the index of the minimum integer in the array, or -1 if the array is empty. int minVal = Integer.MAX_VALUE; int minInd = -1; for (int i = 0; i < a.length; i++) { if (a[i] < minVal) { minVal = a[i]; minInd = i; } } return minInd; } public static int minIndexBetween (int [ ] a, int lo, int hi) { // Return the index of the minimum integer in a[lo..hi], or -1 if the segment is empty int minVal = Integer.MAX_VALUE; int minInd = -1; for (int i = lo; i <= hi; i++) { if (a[i] < minVal) { minVal = a[i]; minInd = i; } } return minInd; } public static void swap (int [ ] a, int i, int j) { // Swap the contents of a[i] and a[j] int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void selectionSort (int [ ] a) { selectionSortBetween(a, 0, a.length-1); } public static void selectionSortBetween (int [ ] a, int lo, int hi) { for (int i = lo; i < hi; i++) { // Note last is not visited. Can you explain why? swap(a, i, minIndexBetween(a, i, hi)); } } public static void insertBetween(int [ ] a, int lo, int hi) { // Assume a[lo..hi-1] is in sorted order. // Insert a[hi] into the segment a[lo..hi] in such a way to make it sorted. int val = a[hi]; int index = hi; while ((index > lo) && (a[index-1] > val)) { // Order of tests is crucial! a[index] = a[index-1]; // Shift value right = shift hole left. index = index - 1; } // At this point, either (index = 0) or ((index > 1) && (a[index-1] <= val)) a[index] = val; } public static void insertionSort (int [ ] a) { insertionSortBetween(a, 0, a.length-1); } public static void insertionSortBetween (int [ ] a, int lo, int hi) { for (int i = lo+1; i <= hi; i++) { insertBetween(a, lo, i); } } }