خورزميات اسئله3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter 2: Revision & Examples

Ex. 1: Write a data list that results from running the shuffle-left algorithm to clean up the
following data. Find the exact number of copies done.

3002670051

Solution: There are 10 numbers in this list, four of which are zero, meaning invalid. Position 2 is a
zero, meaning that every cell from 3 to 10 needs to be copied left. After this first copy, we have
the following list:

3026700511 and legit = 9.

Thus we see that eight copies have been performed on this first step. Unfortunately, another zero
has been shifted into position 2, meaning that we need to make another eight copies to produce
the list:

3267005111 and legit = 8.

Now we see a zero in position 5, meaning that we need to copy cells 6 through 10 left:

3267051111 and legit = 7.

This has performed five copies, but now we must perform five more because of the last zero in
position 5:

3267511111 and legit = 6.

Thus there have been 8 + 8 + 5 + 5 = 26 copies done.

Note that the algorithm is particularly inefficient here in making extra copies which were not
necessary – since legit gets reduced to 9, there is no need to copy position 10 into position 9.
When legit gets reduced to 8, there is no need to copy 10 into 9 and 9 into 8, and so forth Thus
there are six useless copies in this algorithm execution.

Once item n has been copied one cell left, it need not be copied again. Similarly, once item n
– 1 has been copied one cell left, it need not be copied again. The value of legit shows how
many cells from the right end have been copied; the algorithm can be changed to “while right is
less than or equal to legit." 8 + 7 + 3 + 2 = 20 copies

Ex. 2: Write the resulting data list and find the exact number of copies done by the converging-
pointers algorithm when it is executed on the data in Example 1.

3002670051

Solution: In this algorithm, the following swaps are performed:


The 0 in position 2 is swapped with the 1 in position 10 (legit = 9).
The 0 in position 3 is swapped with the 5 in position 9 (legit = 8).
The 0 in position 7 is swapped with the 0 in position 8 (legit = 7),
Then the algorithm takes note that the pointers have converged, and there is still a 0 in position
7 (legit = 6), and stops. There have been three total copies made.

Ex. 3: Write the resulting data list and find the exact number of copies done by the copy over
algorithm when it is executed on the data in Example 1.

Solution:
6 copies to create 2nd list.

Ex. 4: Are each of the following true or false?

1) 3 n^2 + 10 n log n = O(n log n)


2) 3 n^2 + 10 n log n = Omega(n^2)
3) 3 n^2 + 10 n log n = Theta(n^2)
4) n log n + n/2 = O(n)
5) 10 SQRT(n) + log n = O(n)
6) SQRT(n) + log n = O(log n)
7) SQRT(n) + log n = Theta(log n)
8) SQRT(n) + log n = Theta(n)
9) 2 SQRT(n) + log n = Theta(SQRT(n))
10) SQRT(n) + log n = Omega(1)
11) SQRT(n) + log n = Omega(log n)
12) SQRT(n) + log n = Omega(n)

Solution
1) False, since n^2 (the dominate term on the left) is asymptotically faster growing than n log n and
hence not upperbounded by it.
(2,3) True, since n^2 (the dominate term on the left) asymptotically grows like n^2 and hence it is
Omega(n^2) and also Theta(n^2). faster growing than n log n and hence not upper bounded by it.
4)False since n log n (the dominate term on the left) is not asymptotically upper bounded by n.
5)True, since the dominate term on the left, 10 SQRT(n), is asymptotically upper bounded by n.
(6,7) False, since the dominate term on the left, SQRT(n), is not asymptotically upper bounded by n.
See the class notes where we showed that that lim as n -> infinity of log n / SQRT(n) = 0 giving that
SQRT(n) is asymptotically faster growing.
(8) False, since the dominate term on the left, SQRT(n), is asymptotically faster slower growing than n.
(9) True, since the dominate term on the left, 2 SQRT(n), grows asymptotically at the same rate as
SQRT(n).
(10) True, since the dominate term on the left, SQRT(n), is asymptotically faster growing than 1.
(11) True, since the dominate term on the left, SQRT(n), is asymptotically faster growing than log n.
(12) False, since the dominate term on the left, SQRT(n), is asymptotically slower growing than n.

Ex. 5: Give the best asymptotic (“big-Oh”) characterization of the worst case and the best case time
complexities of the algorithm DoAgain(A, n).

Algorithm DoAgain(A,n)
Input: Array A storing integers and of size n > 1.
sum ← 0
for c ← 0 to n2 do
if A[0] < 0 then
for k ← 0 to n − 1 do
sum ← sum + c · A[k]
a) Best case O(n) and worst case O(n2) b) Best case O(n) and worst case O(n3)

c) Best case O(n2) and worst case O(n3) d) Best case O(n) and worst case O(n4)

e) Best case O(n3 ) and worst case O(n4) f) Best case O(n4) and worst case O(n4)

Ex. 6: Use big-O notation to summarize the asymptotic behavior of the following function:

void test ( )
for(int s=1; s<n;s++){
for(int t=1; t<n;t=t*6){
System.out.println(t);
}
}

a) O(nlogn) b) O(logn) c) O(loglogn)

d) O(n2logn) e) O(n) f) O(n2)

Ex. 7: Find the asymptotic behavior (Big-Oh) of the following function:

void silly(int n) {
for (inti = 0; i< n * n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k <i; ++k)
System.out.println(”k = ” + k);
for (int m = 0; m < 100; ++m)
System.out.println(”m = ” + m);
}
}
}
a) O(n5) b) O(n4) c) O(n6) d) O(n2) e) O(logn3) f) O(n3)

You might also like