HW 6

advertisement
CS 103, Homework Assignment 6
Your assignment is to do all 10 problems.
Searching problems
1. [Answer in English] Consider the list: L = [4 5 12 14 17 24 25 27 28
31 35 36 39].
Suppose you are using the binary search algorithm. For each of the following
targets, give a list of the numbers in B (not their indices) that would be compared
with the target in the order in which they would be compared with it.
a.
b.
c.
d.
24
4
17
15
2. [Answer in English] Suppose the binary-search algorithms are applied to unsorted
lists:
a. Will the recursive version ever respond that a target is on the list, when
that target is in fact not on the list?
b. Will the recursive version ever give the wrong index for a target that is on
the list?
c. When the recursive version responds that a target is not on the list, will it
ever be wrong?
d. Will the iterative version ever respond that a target is on the list, when that
target is in fact not on the list?
e. Will the iterative version ever give the wrong index for a target that is on
the list?
f. When the iterative version responds that a target is not on the list, will it
ever be wrong?
Sorting Problems
3. [Answer in English] Suppose that in the function, selection_sort, the
following portion,
if m_min ~= m
temp = v(m);
v(m) = v(m_min);
v(m_min) = temp;
end
is replaced by
Homework Assignment 6
2
temp = v(m);
v(m) = v(m_min);
v(m_min) = temp;
a. How will the output be changed?
b. Will the average number of swaps be increased, decreased, or unchanged?
c. Will the number of statement executions required for a sort always be
increased, always be decreased, or is it indeterminate (i.e., three possible
answers).
4. [Answer in English] The sort function, selection_sort, which is given in
Chapter 10, is applied to each of the following vectors. In each case, give the
vector after the 3rd swap.
a. 621, 687, 74, 479, 118, 199, 142, 467, 445
b. 136, 20, 28, 82, 345, 334, 303, 939, 298, 947
c. 249, 941, 705, 531, 718, 964, 449, 660, 478, 928, 841
d. 242, 853, 227, 860, 728, 140, 395, 148, 230, 883, 778, 561
5. [Answer in English] In the merge_sort function given in Chapter 10, the
subfunction merge_sorted_lists(v1,v2) combines v1 and v2 into one
list that is sorted. Here is a partial listing of that function:
function x = merge_sorted_lists(x1,x2)
% The elements of x1 and x2 are assumed to be
% in ascending order. Combine them into
% ascending order and return them in x.
x = [];
while ~isempty(x1) & ~isempty(x2)
if x1(1) < x2(1)
x = [x, x1(1)]; % add x1(1) to x
x1 = Answer (a);
else
x = [x, x2(1)]; % add x2(1) to x
x2 = Answer (b);
end
end
x = [x,x1,x2];
a. What goes in place of Answer (a)?
b. What goes in place of Answer (b)?
Homework Assignment 6
3
Problems on algorithmic complexity
6. [Answer in English]. Listed below is the worst-case time behavior t  N  of some
fictitious algorithms, where N is the size of the input. Give the order of each
algorithm using “Big-Oh” notation:
a. 2 N 3  14 N 2  3
b. a  b log  N 3 
c. 6log  N   2log  log  N  
d. 4  3.2N  2 N
Problems on symbolic mathematics:
Problems 7 through 9 have to do with the following situation, depicted in the figure:
A car is traveling at night along a highway. The highway is shaped like a parabola with
its vertex at the origin. The car starts at a point 100 meters west and 100 meters north of
the origin and travels in an easterly direction. There is a statue located 100m east and
50m north of the origin. Let x represent the distance of the car east from the origin
(negative values of x indicate that the car is west of the origin); let y represent the
distance of the car north from the origin.
(-100, 100)
N
(100, 50)
W
E
S
Origin
(x , y)
Homework Assignment 6
4
7. [Answer in English]. Give an equation that relates y to x. (Hint: We are simply
asking for the equation of a parabola. You can look it up anywhere, so this
problem does not count much.)
8. [Answer in Matlab] Using Matlab’s symbolic mathematics facility, give
commands that will determine the position of the car on the highway at which the
car’s headlights will illuminate the statue. Assume that the statue is a point object.
9. [Answer in English]. What is the equation of the headlight’s illumination when it
strikes the statue? Give the numerical values of the coefficient and the constant in
this equation. You will probably need to use Matlab to find them.
10. [Answer in Matlab] When gas expands in a cylinder with radius r, the pressure at
any given time is a function of the volume: P = P(V). The force exerted by the
gas on the piston (as illustrated in the figure below) is the product of the pressure
and the area:
V
x
F = r2P. The work done by the gas when the volume expands from V1 to
volume V2 is therefore:

V1
V2
PdV
In a steam engine the pressure P and volume V of steam satisfy the equation PV1.4
= k, where k is a constant. (This is true for adiabatic expansion – that is,
expansion in which there is no heat transfer between the cylinder and its
surroundings.) Using Matlab’s symbolic mathematics facility, give commands
that calculate the work done by the engine during a cycle when the steam starts at
a pressure of 160 lb/in2 and a volume of 100 in3 and expands to a volume of 800
in3.
Download