Chapter 12—Arrays Continued

advertisement
Fundamentals of Java
Chapter 12: Arrays Continued
UNIT 3— ARRAYS, RECURSION, AND COMPLEXITY
CHAPTER 12—ARRAYS CONTINUED
EXERCISE 12.1
1. A linear search is linear because the elements are examined in sequence, starting
with the first one, until the target element is found or the end of the array is
reached.
2.
int search (Object[] a, Object searchValue){
for (i = 0; i < a.length; i++)
if (a[i].equals(searchValue))
return i;
return -1;
}
3. The elements examined are 78, 85, and 99.
4.
int search (int[] a, int searchValue){
for (i = 0; i < a.length; i++)
if (a[i].== searchValue)
return i;
else if (a[i] < searchValue)
return –1;
return -1;
}
5. This code segment determines whether or not the elements in the array are in
ascending order. If they are, the variable inOrder is true at the end of the loop; if
they are not, the variable is false.
EXERCISE 12.2
1. 87654 (initial order)
47658
45678
45678 (no movement was necessary)
45678 (no movement was necessary)
2. 87654 (initial order)
78654
76854
76584
76548
3. With an array that is already sorted, the selection sort still makes all passes
through both loops, but does not swap any elements. The bubble sort makes one
pass through the outer loop, determines that no swaps were made, and stops at
1
Fundamentals of Java
Chapter 12: Arrays Continued
that point. The insertion sort makes one pass through the outer loop and finds the
location of the item to insert at its current position. Thus, no swaps are made.
4. We assume that there is a method swap(Object a, int x, int y).
void bubbleSort(Object[] a){
int k = 0;
boolean exchangeMade = true;
// Make up to n - 1 passes through array, exit early if no exchanges
// are made on previous pass
while ((k < a.length() - 1) && exchangeMade){
exchangeMade = false;
k++;
for (int j = 0; j < a.length() - k; j++)
if (((Comparable)a[j]).compareTo(a[j + 1]) > 0){
swap(a, j, j + 1);
exchangeMade = true;
}
}
}
EXERCISE 12.3
1. To insert an element into an array, the logical size must be less than the physical
size and the target position must be greater than or equal to zero and less than or
equal to the logical size. If these conditions are true, we shift the elements down
by one position, starting with the last element, until we shift the element at the
target position. Then, we set the target cell to the new element.
2.
static public boolean insertItem(Object[] array, int logicalSize,
int targetIndex, Object newItem){
// Check for a full array and return false if full
if (logicalSize == array.length)
return false;
// Check for valid target index return false if not valid
if (targetIndex < 0 || targetIndex > logicalSize)
return false;
// Shift items down by one position
for (int i = logicalSize; i > targetIndex; i--)
array[i] = array[i - 1];
// Add new item and return true
array[targetIndex] = newItem;
return true;
}
static public boolean removeItem(Object[] array, int logicalSize,
int targetIndex){
// Check for valid target index return false if not valid
if (targetIndex < 0 || targetIndex >= logicalSize)
return false;
// Shift items up by one position
for (int i = targetIndex; i < logicalSize - 1; i++)
2
Fundamentals of Java
Chapter 12: Arrays Continued
array[i] = array[i + 1];
// Return true
return true;
}
3. Code segment a) determines whether or not an array is full and doubles its size if
more cells are needed. Code segment b) determines whether or not array cells are
being wasted, using the condition that the logical size is ¼ of the physical size. If
so, the physical size of the array is reduced by ½.
EXERCISE 12.4
1. A two-dimensional array organizes items in rows and columns. Each item is
accessed by specifying two index positions.
2. int array[][] = new int[10][20];.
3.
int row = 0;
int col = 0;
boolean found = false;
for (row = 0; row < a.length; row++){
for (col = 0; col < a[row].length; col++)
if (a[row][col] < 0){
found = true;
break;
}
if (found)
break;
}
4. This code segment produces an array in which each cell contains the product of its
row and column. Thus, for example, the cells in row 0 contain 0. The cells in row
1 contain 0-4, and the cells in row 2 contain 0, 2, 4, 6, and 8.
5. for (int row = 0; row < table.length; row++){
for (int column = 0; column < table[row].length; column++)
System.out.print(table[row][column] + " ");
System.out.println();
}
REVIEW QUESTIONS
Multiple Choice
1.
2.
3.
4.
5.
b
b
a
a
a
3
Download