CMPS 134
Sample test questions regarding arrays

1. Consider the following array of int's. (Notice that its elements are in ascending order.)
   0    1    2    3    4    5    6    7    8    9   10   11   12   13   14
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| -4 | -1 |  0 |  3 |  5 |  5 |  8 | 12 | 15 | 17 | 17 | 21 | 24 | 30 | 32 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

Suppose that the binary search algorithm (as embodied in the binarySearch() method in the ArrayExamples class) is applied to it in search of 20. Show the initial values of the variables low and high. For each loop iteration, identify the location of the array element that was "probed" during that iteration as well as the values of low and high at the end of that iteration.

Solution:

When                 low    high     location of probed element
--------------      -----  ------    --------------------------
initially             0      15
1st iteration         8      15               7
2nd iteration         8      11              11
3rd iteration        10      11               9
4th iteration        11      11              10

2. Repeat the previous exercise, except this time assume that the value being sought is 5.

Solution:

When                 low    high     location of probed element
--------------      -----  ------    --------------------------
initially             0      15
1st iteration         0       7               7
2nd iteration         4       7               3
3rd iteration         4       5               5
4th iteration         4       4               4

3. Consider the following array of int's.

   0    1    2    3    4    5    6    7    8    9  
+----+----+----+----+----+----+----+----+----+----+
|  4 | 17 |  9 | 35 | 15 | 23 |  8 |  1 | 19 |  3 |
+----+----+----+----+----+----+----+----+----+----+

Suppose that the selection sort algorithm (as embodied in the selectionSort() method in the ArrayExamples class) is applied to it. Show the contents of the array at the end of each of the first four iterations of the (outer) loop.

Solution:

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 1st iteration:  |  1 | 17 |  9 | 35 | 15 | 23 |  8 |  4 | 19 |  3 |
                      +----+----+----+----+----+----+----+----+----+----+

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 2nd iteration:  |  1 |  3 |  9 | 35 | 15 | 23 |  8 |  4 | 19 | 17 |
                      +----+----+----+----+----+----+----+----+----+----+

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 3rd iteration:  |  1 |  3 |  4 | 35 | 15 | 23 |  8 |  9 | 19 | 17 |
                      +----+----+----+----+----+----+----+----+----+----+

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 4th iteration:  |  1 |  3 |  4 |  8 | 15 | 23 | 35 |  9 | 19 | 17 |
                      +----+----+----+----+----+----+----+----+----+----+

4. Suppose that we are applying the insertion sort algorithm (as embodied in the insertionSort() method in the ArrayExamples class) to an array, and, after three iterations of its loop, the array is as follows:

   0    1    2    3    4    5    6    7    8    9  
+----+----+----+----+----+----+----+----+----+----+
|  4 |  9 | 17 | 35 | 15 | 23 |  8 |  1 | 19 |  3 |
+----+----+----+----+----+----+----+----+----+----+

Show the contents of the array at the end of each of the next three iterations (i.e., the 4th, 5th, and 6th) of the loop.

Solution: In general, the j-th iteration of the loop "inserts" (hence the name of the algorithm) the value at location j into its rightful place among the elements in locations 0..j.

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 4th iteration:  |  4 |  9 | 15 | 17 | 35 | 23 |  8 |  1 | 19 |  3 |
                      +----+----+----+----+----+----+----+----+----+----+

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 5th iteration:  |  4 |  9 | 15 | 17 | 23 | 35 |  8 |  1 | 19 |  3 |
                      +----+----+----+----+----+----+----+----+----+----+

                         0    1    2    3    4    5    6    7    8    9  
                      +----+----+----+----+----+----+----+----+----+----+
after 6th iteration:  |  4 |  8 |  9 | 15 | 17 | 23 | 35 |  1 | 19 |  3 |
                      +----+----+----+----+----+----+----+----+----+----+