Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Edit the code so that the output would match the following CPS In Class Assignment 2 by __________ Array before sorting: 35 22 60 79
Edit the code so that the output would match the following CPS In Class Assignment 2 by __________ Array before sorting: 35 22 60 79 44 63 99 34 19 30 Array after pass 1: 22 35 60 79 44 63 99 34 19 30 Array after pass 2: 22 35 60 79 44 63 99 34 19 30 Array after pass 3: 22 35 60 79 44 63 99 34 19 30 Array after pass 4: 22 35 44 60 79 63 99 34 19 30 Array after pass 5: 22 35 44 60 63 79 99 34 19 30 Array after pass 6: 22 35 44 60 63 79 99 34 19 30 Array after pass 7: 22 34 35 44 60 63 79 99 19 30 Array after pass 8: 19 22 34 35 44 60 63 79 99 30 Array after pass 9: 19 22 30 34 35 44 60 63 79 99 30 Array after sorting: 19 22 30 34 35 44 60 63 79 99 30 BUILD SUCCESSFUL (total time: 0 seconds) 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, " Array before sorting"); sort(intList); printList(intList, MAX_SIZE, " Array 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, " After pass " + k); } // end for } // end sort // find the right place for item to be inserted within arr[0:size-1] private static int findPos(int[] arr, int size, int item) { // write the code return 0; // stub, keeps compiler happy } // end findPos
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started