Sample Mid-Term Exam

advertisement
Spring 1, 2001
Algorithms
Name: __________________
____ / 172 = _____ %
MID-TERM EXAM
Part 1: Multiple Choice. Choose BEST answer. [2 pts each]
1). You are told that you need to search on a set of data that will not be sorted, the maximum array size
will remain constant, and you need the fastest search possible. It does not matter if the storage method
maintains the order of the data or not. Which lookup would you use:
a).
linear search
b).
sequential search
c).
binary search
d).
hash lookup
2).
You are told that you need to search on a set of data that must stay sorted, the maxium size of the
array will remain constant, and you need the fastest search possible. You also need the storage method to
maintain the order of the data. Which lookup would you use:
a).
linear search
b).
ordered linear search (uses ordered shortcut)
c).
binary search
d).
hash lookup.
3).
You are told that you need to search on a set of data that may or may not be sorted, but you must
preserve the chronological order of the data. That is, the order in which the data was inserted into storage.
Which lookup would you use:
a).
linear search
b).
ordered linear search
c).
binary search
d).
hash lookup
4).
You are told that your system is going to use a hash-table for lookups. Being a computer savvy
individual, you know that means that one of the following is true, which is it:
a).
The system makes great hash-browns.
b).
You will have a difficult time retrieving a list of data in its original order.
c).
Adding items will be very difficult and much slower than a lookup.
d).
Looking up items is very slow.
e).
At best case, the look-up time and insertion time is O(n).
5).
You are being told that your system is going to use a sequential search to lookup data, being a
computer savvy individual, you know that means one of the following is true, which is it:
a).
It will contain shiny little disks that sparkle in the light.
b).
If the data is not sorted, there are faster ways to search the data.
c).
At worst case, you’ll need to examine every item in the list.
d).
At best case, the lookup time is O(n log2 n)
Spring 1, 2001
Algorithms
Name: __________________
6).
You are being told that your system is going to use a binary search to lookup unordered data,
being a computer savvy individual, you know that means one of the following is true, which is it:
a).
At best case, the lookup time is linear.
b).
If the data is in the list, lookup time is O(n log2n), but if not, it is O(n)
c).
You want to shoot the system architect, because this cannot be done (easily).
d).
To use a binary search, you need more space than the actual number of items.
7).
Spring 1, 2001
Algorithms
Name: __________________
Part 2, Short Answer. [Pts vary per question]
12). [2 pts] When you hash a key to get an address, what is the name of the area where the data is placed
in the hash table?
13). [4 pts] What is the difference between primary and secondary clustering?
14). [4 pts] Given the hash table below (where no keys are shown), shade in any 4 cells to illustrate
primary clustering:
Typical collision search path
0
1
2
3
4
5
6
7
8
9
15). [4 pts] Given the hash table below (where no keys are shown), shade in any 4 cells to illustrate
secondary clustering:
Typical collision search path
0
1
2
3
4
5
6
7
8
9
Spring 1, 2001
Algorithms
Name: __________________
16). [2 pts] What is the name of the hashing method where the key is the same as the address?
17). [6 pts] Name 2 different collision resolution methods for hashing:
18). [6 pts] Name 2 different hashing methods and give an example of a key and an address for each:
19). [2 pts] To keep our hashtable at peak efficiency, what is the typical percent full threshold we do not
wish to cross?
22). [4 pts] In linked list hashing, where do the collisions for an address go? What do we call in hash
terms this area of memory of the hash table?
23). [4 pts] What is the difference between an ordered sequential search, and a binary search? Which is
more efficient?
Spring 1, 2001
Algorithms
Name: __________________
Part 3, Problem Solving
24).
[12 Pts] What is logically wrong (if anything) with the following recursive functions:
int sigma(int x)
{
return x + sigma(x-1);
}
int sigma2(int x)
{
if(x == 0)
return 0;
else
return x + x-1;
}
int sigma3(int x)
{
if(x == 0)
return 0;
else
return x + sigma3(x+1);
}
int sigma4(int x)
{
if(x <= 0)
return 0;
else
return x + sigma4(x-1);
}
Spring 1, 2001
Algorithms
Name: __________________
25).
[12 pts] Trace the following recursive function, and give the output for the calls below.
int foo(int x)
{
if(x <= 0)
return 0;
else
return x + foo(x-1);
}
// integer division
Given the following calls, trace the recursion to arrive at the answer:
foo(4);
foo(2);
foo(6);
Spring 1, 2001
Algorithms
Name: __________________
30). [10 pts] Use a binary search to find the value 62 in the list below. Update the values of H,
M, and L with each pass.
Pass = 1
Pass = 2
Pass = 3
Pass = 4
Pass = 5
19
27
39
47
52
59
60
65
31). [10 pts] Use a binary search to find the value 19 in the list below. Update the values of H,
M, and L with each pass.
Pass = 1
Pass = 2
Pass = 3
Pass = 4
Pass = 5
19
27
39
47
52
59
60
65
32). [2 pts] What must be true about a list of values before you can perform a binary search?
33). [40 pts] You are told to write a function that computes the sigma of a range of numbers.
The mathematical Sigma stands for a summation of all numbers from the lowest to highest
Spring 1, 2001
Algorithms
Name: __________________
For example:
Sigma(3,9) = 3 + 4 + 5 + 6 + 7 + 8 + 9 = 42
Write a recursive summation function that will be able to process summations from a min to a
max value. Do not worry about error conditions, assume min is less than or equal to max.
Spring 1, 2001
Algorithms
Name: __________________
Part 1, Problem Solving
1).
Data:
[10 pts] Given the following data in an array. Perform (by hand) an insertion sort on the
data, showing the data at each pass through the sort. Note when sort stops.
8
22
1
3
23
31
19
13
7
2
Pass
1
Pass 2
Pass 3
Pass 4
Pass 5
Pass 6
Pass 7
Pass 8
Pass 9
Pass 10
3).
Data:
Pass
1
Pass 2
Pass 3
Pass 4
Pass 5
Pass 6
Pass 7
Pass 8
[10 pts] Given the following data in an array. Perform (by hand) a selection sort on the
data, showing the data at each pass through the sort. Note when sort stops.
8
22
1
3
23
31
19
13
7
2
Spring 1, 2001
Algorithms
Name: __________________
Pass 9
Pass 10
Part II: Multiple Guess. Choose BEST answer. [2 pts each]
10). Which sort would stop immediately after 1 pass if the data were already sorted?
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
11).
Of the following sorts, which one is the worst in terms of complexity theory?
a).
bubble sort
b).
quick sort
c).
shell sort
d).
None, they are all O(n2) sorts.
12).
Which sort does C++ have built into its language library?
a).
shell sort
b).
bubble sort
c).
quick sort
d).
heap sort
13). Which sort operates by dividing a list into two halves, with all elements smaller than the
median on one side, and all greater than the median on the other?
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
14). Which sort operates by comparing consecutive pairs of numbers and swapping those that
are not arranged properly.
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
15). Which sort operates like a person picking up one card at a time, and inserting it into the
proper place in her/his hand?
Spring 1, 2001
Algorithms
Name: __________________
a).
b).
c).
d).
bubble sort
selection sort
insertion sort
quick sort
16). Which sort divides a list into intervals, which are spread throughout the list. These
intervals contract after each pass.
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
shell sort
17). Which one of the following sorts will guarantee that the smallest item is in position 0 of
the array after the first pass?
a).
bubble sort
b).
insertion sort
c).
selection sort
d).
quick sort
18). Which sort guarantees that after n passes, the first n items in the list are sorted, but only in
relation to themselves.
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
e).
shell sort
Download