This assignment uses some concepts developed in ICA 1. In particular, the shiftDown method developed for ICA 1 can be reused. When completed, this program will create a random array with 10 integer values and sort it using Insertion sort technique. Most of the code is written. You should be able to reuse code for shiftDown method. You need to complete the findPos method, following the description given later. Submit the source code and screen shot as described in the document "How to submit assignments" through Isidore. Follow naming conventions as already described. Design of "find Pos" method The findPos method is used to find the right place to insert a new value in a partially filled array. The partially filled array is assumed to be sorted in increasing order and if the new value is inserted at the index position (as calculated by findPos) then the resulting array will remain sorted. This is the method specification given for findPos: 6 18 35 45 // find the right private static int Suppose size = 5 and arr = {12, 23, 45, 67, 88). Note that there may be more elements in arx but as a partially filled array, only 5 values are relevant. These are the values returned by the method for various possible values of item. item 56 77 91 place for item to be inserted within arr [0:size-1] findPos (int[] arr, int size, int item) { return value from findPos (arr, 5, item) 0 1 2 3 (new 45 goes after old 45's) 3 4 5 You can start a position variable at 0 and keep incrementing it as long as the position is < size and item is 2 arr[position]. The final value is the return value from the method. Note: It is also possible to write the loop backward.
java
final static Random rand = new Random(); final static int MAX_SIZE = 10; // amount of storage for the intList static int size = 0; // number of values actually in the intList static int[] intList = new int[MAX_SIZE]; /** * @param args the command line arguments */ public static void main(String[] args) { out.println("CPS 151 In-class assignment 2 by _______________________"); setRandom(intList, 100); printList(intList, MAX_SIZE, "\nArray before sorting"); sort(intList); printList(intList, MAX_SIZE, "\nArray after sorting"); } // end main // prints partially filled array with a legend private static void printList(final int[] arr, final int size, final String legend) { out.println(legend); for (int k = 0; k < size; k++) { out.print(" " + arr[k]); } out.println(); } // end printList // move items from pos:size-1 one position down (higher subscripts) private static void shiftDown(int[] arr, int size, int pos) { // Write the code (use code from ICA 1) } // end shiftDown private static void setRandom(int[] arr, final int range) { for (int k = 0; k < arr.length; k++) { arr[k] = rand.nextInt(range); } } // end setRandom private static void sort(int[] arr) { for (int k = 1; k < arr.length; k++) { // assume arr[0:k-1] sorted int save = arr[k]; // arr[k] may be overwritten later, so save that value int pos = findPos(arr, k, save); // find where to insert save in arr[0:k-1] shiftDown(arr, k, pos); arr[pos] = save; // arr[0:k] is now sorted with k+1 values printList(arr, MAX_SIZE, "\nAfter pass " + k); } // end for } // end sort
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images