Java Reference
In-Depth Information
return result;
}
b. public static void mystery2(int[] list) {
for (int i = 0; i < list.length / 2; i++) {
int j = list.length - 1 - i;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
c. public static void mystery3(List<String> list) {
for (int i = 0; i < list.size() - 1; i += 2) {
String first = list.remove(i);
list.add(i + 1, first);
}
}
d. public static void mystery4(List<String> list) {
for (int i = 0; i < list.size() - 1; i += 2) {
String first = list.get(i);
list.set(i, list.get(i + 1));
list.set(i + 1, first);
}
}
3. Suppose the following arrays have been declared:
int[] numbers1 = {63, 9, 45, 72, 27, 18, 54, 36};
int[] numbers2 = {37, 29, 19, 48, 23, 55, 74, 12};
int[] numbers3 = {8, 5, -9, 14, 0, -1, -7, 3};
int[] numbers4 = {15, 56, 24, 5, 39, -4, 27, 10};
a. Write the state of the elements of each array after each pass of the outermost loop of the selection sort algorithm
has occurred (after each element is selected and moved into place).
b. Trace the complete execution of the merge sort algorithm when called on each array. Show the subarrays that are
created by the algorithm and show the merging of subarrays into larger sorted arrays.
4. Write code to read a dictionary from a file, then prompt the user for two words and tell the user how many words in
the dictionary fall between those two words. Here is a sample run of the program:
Type two words: goodbye hello
There are 4418 words between goodbye and hello
Use the binary search algorithm in your solution.
5. Write a Comparator that compares Point objects by their distance from the origin of (0, 0). Points that are closer
to the origin are considered to come before those which are further from the origin.
Search WWH ::




Custom Search