Java Reference
In-Depth Information
b.
int sum = 0;
for (int j = 1; j < n; j++) {
sum++;
if (j % 2 == 0) {
sum++;
}
}
c.
int sum = 0;
for (int i = 1; i <= n * 2; i++) {
for (int j = 1; j <= n; j++) {
sum++;
}
}
d.
for (int j = 1; j < 100; j++) {
sum++;
sum++;
}
5.
Determine the complexity classes of the algorithms that could be used to perform the following tasks:
a. Finding the average of the numbers in an array of integers
b. Finding the closest distance between any pair of points in an array of
Point
s
c. Finding the maximum value in an array of real numbers
d. Counting the median length of the
String
s in an array
e. Raising an integer to a power—for example, A
B
f. Examining an array of
Point
s to see how many trios of points are colinear—that is, how many groups of three
points could be connected by a straight line
g. Counting the number of lines in a file
h. Determining whether a given integer representing a year stores a leap year (a year divisible by 4, but not divisible
by 100 unless also divisible by 400)
Section 13.3: Implementing Searching and Sorting Algorithms
6.
What is the runtime complexity class of a sequential search on an unsorted array? What is the runtime complexity
class of the modified sequential search on a sorted array?
7.
Why does the binary search algorithm require the input to be sorted?
8.
How many elements (at most) does a binary search examine if the array contains 60 elements?
9.
What indexes will be examined as the middle element by a binary search for the target value
8
when the search is
run on the following input arrays? What value will the binary search algorithm return?
a.
int[] numbers = {1, 3, 6, 7, 8, 10, 15, 20, 30};
b.
int[] numbers = {1, 2, 3, 4, 5, 7, 8, 9, 10};
c.
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
d.
int[] numbers = {8, 9, 12, 14, 15, 17, 19, 25, 31};
Search WWH ::
Custom Search