SOLUTION:
. Looking at the factors of the first term
yields,
,
, and
. Multiplying these yields
which is bigger than O(n2).
SOLUTION:
t(n) = O(n4).
SOLUTION:
(6): This one is tricky! The innermost loop is only reached i
times for each value of j -- once each time j is a multiple of
i. The if
test, however, is done for each value of j.
Thus, I have broken up the count of the for ( int j ...
loop
into two parts -- one for the if
test and one for the
innermost for
loop, which is executed in its entirety once each
time j is a multiple of i. The latter can be written as a summation of
i, i2, i3, etc. Together, these give
Hello
is output by each of the following code fragments.
Obtain an accurate``O'' estimate from the summation.
for ( i=1; i<=n; i++ ) for ( j=1; j<=i; j++ ) for ( k=j+1; k<=n; k++ ) cout << "Hello\n";SOLUTION:
Hello
is output O(n3) times.
2^i
means 2i.
for ( i=0; i<=k; i++ ) for ( j=2^i+1; j<=n; j++ ) cout << "Hello\n";SOLUTION:
SOLUTION:
This problem is better as a homework problem than as a quiz problem,
but it is still a good exercise...
The simplest solution is to just test each entry of the list. This clearly requires O(n) time. There is a faster way.
The following algorithm requires time because it is
essentially the same as binary search. The trick depends on two
facts: the array holds integers and the array is in strictly
increasing order (there are no duplicates). To see the trick,
consider what we know if a[i] < i for some i. Then, because of
the two facts,
, so a[i-1]<i-1.
Similarly, we can show that a[i-2] < i-2, and so on, inductively, to
show that a[j]<j for
. Similarly, we can show that
if a[i] > i, then a[j] > j for
. This gives
the following algorithm:
int FindEqual( int pts[], int n, int & loc ) { int low = 0, high = n-1, mid; while ( low <= high ) { mid = (low + high) / 2; if ( pts[mid] == mid ) { loc = mid; return true; } else if ( pts[mid] < mid ) low = mid+1; else high = mid-1; } return false; // at this point low > high and there is // nothing left to search }Since the search range in the array is split in half each time, at most
2.9, 3.5, 1.1, 6.1, 2.3, 1.8, 8.7, 3.0, 2.4,the algorithm should return 0.1, which is the difference between 3.0 and 2.9. Make your algorithm as efficient as you can and give the worst-case running time of your algorithm as a function of n, briefly justifying your answer. Hint: you may change or reorganize the contents of the array.
SOLUTION:
The trick to this algorithm is to simply sort the values into
increasing order, which we know we can do in time using
Merge Sort (among others), and then examine the difference between
adjacent elements of the list to find the smallest difference. This
yields the following algorithm.
float Smallest_Gap( float a[], int n ) { sort a into increasing order, for example using MergeSort; float smallest = a[1]-a[0]; // assume n >= 2 for ( i = 1; i<n-1; i++ ) if ( a[i+1] - a[i] < smallest ) smallest = a[i+1] - a[i]; return smallest; }Since sorting requires
for
loop
requires just O(n) time, the overall cost is