---400---
/ \
-200- 500
/ \ / \
150 325 450 600
/ \ \
50 170 375
/
160
-300-
/ \
175 400
/ \
100 250
/ / \
50 200 275
--100--
/ \
40 200
/ \ / \
20 60 150 400
/ \
125 175
/
160
Euclid's algorithm is a recursive algorithm for finding the gcd of two integers a and b that can be expressed as follows:
if a > b, gcd(a, b) =
if a < b, gcd(a, b) =
int i, j;
int sum = 0;
for (i = 0; i < n; i++)
sum += i;
for (j = 0; j < n/2; j++)
sum *= j;
|
int search(vector<string>names, string target) {
int high = names.size()-1, low = 0, mid;
do {
mid = (low + high) / 2;
if (target == names[mid])
return mid;
else if (target < names[mid])
high = mid - 1;
else
low = mid + 1;
} while (low <= high);
return -1;
}
|
|
int i, j;
int sum = 0;
for (i = 0; i < n; i++) {
for (j = 1; j < n; j *= 2) {
sum += a[i][j];
}
}
cout << sum;
|
For each fragment of code, please circle its Big-O running time:
a O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n) b O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n) c O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n) d O(1) O(log n) O(n) O(n log n) O(n2) O(n3) O(2n)
a. array
b. vector
c. stack
d. deque
e. hash table
f. list
g. binary search tree
h. AVL tree
For each of the following questions choose the best answer from the above list. Assume that the size of an array is fixed once it is created, and that its size cannot be changed thereafter. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question:
( 11, 58, 23, 23, 11, 16, 23 )
Our program will report that 2 students scored an 11,
1 student scored a 16, 3 students scored a 23, and 1
student scored a 58. Assume that you
know in advance that the scores range in value
from 0-100.
Dnode *InsertBefore(string value, Dnode *node);value is the string to be inserted into the doubly-linked list and it should be inserted before node. The return value should be a pointer to the newly created node for value. Here is the declaration for Dnode:
class Dnode {
public:
string s;
Dnode *flink;
Dnode *blink;
};
Assume that the list has a sentinel node so you are always guaranteed
to have a predecessor node.