Homework #5, CS110
Due 11/22/2019
Problems are worth 5 points each question/ subquestion
60points total
1. Given the existence of an array called “list” declared as followes:
int[] list = {90,-45,78,3,10,-5,48,36,15,20,-24,100,16,-31,70,-17};
For each subproblem, assume we begin with the original array
a) Show how the contents of the array would be reordered after 3 passes of
the bubble sort
b) Show how the contents of the array would be reordered after 3 passes of
the selection sort. NOT COVERED
c) Show how the contents of the array would be reordered after 3 passes of
the insertion sort NOT COVERED
2. Assume that there exists an array called List containing the following values.
Note index #’s are shown in the second row
-5 -3 0 9 10 18 22 27 31 40 55 67 82 99 10 111 125
2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
USE a trace table to show the execution of the binary search algorithm and
what is RETURNED for the following search keys:
a) 67
b) 105
3. Suppose list is an array of 7 elements of type int. what is stored in the array list
after the following Java code executes?
int[] list = new int[7];
list[0] = 3;
for (int i=1; i< list.length; i++) {
list[i] = i + list[i-1] *10;
if (i < 3)
list[i] = 2 * list[i-1] -i*2;
}
4. Write a method which receives an array of integers. This method should reverse
the contents of the array, inside the original array.
public static void reverseArray( int[] values)
5. Write a method which receives an integer array as a parameter, and prints out the
index numbers of every element which contains a negative number.
public static void printIndexes( int[] values)
6. Consider the following declarations:
int [][] beta = new int[3][3];
int i, j;
What is stored in beta after the following statements executes:
a. for ( i = 0; i < 3; i++)
for (j=0; j < 3; j++)
beta[i][j] = i * j -2;
7. Consider the following declaration:
int[][] alpha = new int[7][6];
a. Write loops to initialize each element of the array alpha, from step 1, to
the value 20.
b. Write loops to store 15 in the first two columns and 20 in the remaining
columns of alpha.
c. Write loops to store the value 2 in the first and last row and first and last
column and 1 in all other cells