Summer 2011
Long Answer
1.
Two
int
arrays,
A
and
B
, contain
m
and
n
int
s each, respectively.
The elements within each of these arrays
appear in ascending order without duplication (i.e. each table represents a set).
Give Java code for a
algorithm
to find the
set difference
by producing a third array
C
(in ascending order) with the values that appear in
A,
but
not
B
,
and
sets the variable
p
to the final number of elements copied to
C
.
(Details of input/output, allocation,
declarations, error checking, comments and style
are unnecessary
.)
15 points
Example:
A
= {1, 2, 4, 5, 7, 11},
B
= {0, 1, 2, 6, 7, 13},
C
= {4, 5, 11},
p
= 3
2.
Use the recursion-tree method to show that
is in .
10 points
3.
Use the substitution method to show that
is in .
10 points
4.
Complete the function by writing the code to replace each
???
on the line to its right.
10 points
public static <T extends Comparable<? super T>> int
binSearchFirst(T[] a,T key) throws ClassCastException
{
// Finds index of first slot with a key >= a given key
// WARNING - Returns n if key>a[n-1]
int low,high,mid,order;
int n=a.length;
low=0;
high=???;
__________________
while (low???high) __________________
{
mid=???;
__________________
order=a[mid].compareTo(key);
if (order???0)
__________________
low=mid+1;
else
high=mid-1;
}
return ???;
__________________
}
5.
Use the efficient construction to convert into a minHeap.
5 points
11
6
1
10
8
4
3
2
5
1
2
3
4
5
6
7
8
9
9
10
7
11
1
2
3
4
5
6
7
8
9
10
11
6.
Show the maxheap after changing the priority at subscript 6 to 9.
5 points