H23 SectionHandout5 PDF
H23 SectionHandout5 PDF
a)
int Mystery1 (int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
sum += i * j;
}
}
return (sum);
}
b)
int Mystery2 (int n)
{
int sum = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < i; j++)
{
sum += j * n;
}
}
return (sum);
}
c)
int Mystery3 ( int n )
{
if( n <= 1 ) return 1;
return (Mystery3( n / 2 ) + 1);
}
int Mystery4(int n)
{
return Pinky(0, n);
}
Say you are trying to choose whether to use selection sort or merge sort to sort some
amount of data. As we saw in class, selection sort has a runtime of O(N2) while merge
sort has a runtime of O(N log N). However, because merge sort requires us to create
temporary data structures to copy values in to and out of during the sorting, merge sort
has higher constant factors than selection sort and for certain input sizes these can
outweigh the advantage of being N log N rather than N2. Assume selection sort has a
constant factor of 10 (that is, the time to run selection sort is less than or equal to 10 * N2)
and assume merge sort has a constant factor of 100.
a.) Which algorithm would you choose if you needed to sort 50 items?
b.) Which algorithm would you choose if you needed to sort 100 items?
Based on the above, if you were writing a sorting function and could only use merge sort
or selection sort (or a hybrid approach), what would you do to make it as fast as possible?
Compare and contrast the two search methods as applied to the problem of solving a (not
necessarily perfect) maze. Is one faster than the other? Does one use more memory?
What are their Big O values based on the maze dimensions, l and w (remember that Big
O bounds the worst case)? Do the two algorithms necessarily return the same paths? If
the two algorithms sometimes return different paths, sketch an example maze where they
return different paths.
b) Summing each subvector does a lot of redundant work. For example, if your Vector
contains [2, -5, 12, 9, -3, 10], the subvectors [-5, 12] and [-5, 12, 9, -3] both require
summing -5 and 12. How can you change the algorithm from part a to reuse
computation rather than repeating it? Describe the new version of the algorithm using
pseudocode. The resulting algorithm should be O(N^2)
c) Now consider solving the problem using a divide-and-conquer approach that divides
the vector into two halves to be handled recursively. How are result(s) from each half
combined to solve the larger problem? What is the base case for this algorithm? You
should describe the algorithm using pseudocode. The algorithm should run in O(NlogN)
time.
d) While the previous algorithm is pretty good, there is an extremely clever way to solve
the problem which only uses O(N) time. This means it only examines each element in the
vector some constant number of times, in this case one time. Describe this algorithm
using pseudocode. As a hint, consider the following case: suppose you have determined
what is the greatest sum of any subvector in an existing vector. Then you add a single
element to the end of the vector. If this changes what the subvector with the greates sum
is, what must be true about the new greatest subvector? What extra piece of information
(besides the current greatest sum and the value of the new element) would you need to
immediately determine whether the new element changes the greatest subvector? Now
consider processing the Vector from left to right. How is this like the situation just
described?
e) Finish the following table of run times of the previously described algorithms based on
the big-O time of the algorithms: