1 |
2n2+n is O(n3).
True or false?
|
|
2 |
What does the statement: "f(n) is O(g(n))" mean?
|
|
3 |
Arrange these functions in order of their growth rates (slowest growing
functions first):
- log n
- 1.0005n
- n2
- n!
- sqrt(n)
- nlog n
|
|
4 |
For what values of n is
4 x 106 n2 > 10 x 2n?
|
|
5 |
What is meant by:
s is an O(1) sequence of statements.
|
|
6 |
What is the time complexity of this fragment of code:
for( j=0;j<n;j++ )
for(k=0;k<n;k+=10) s;
|
s is an O(1) sequence of statements.
|
|
7 |
What is the time complexity of this fragment of code:
for( j=0;j<n;j=j*1.1 )
for(k=0;k<n;k++) s;
|
s is an O(1) sequence of statements.
|
|
8 |
What attributes are needed for the class(es) needed to model
the simplest possible list?
|
|
9 |
For the simplest possible list, what is the time complexity for:
- adding to the head,
- adding to the tail,
- deleting a specific item,
- deleting an arbitrary item,
- searching for an item?
|
|
10 |
What modifications (if any) can you make to the simplest possible list
to change any of the time complexities in the previous question?
|
|
11 |
- What structure do you use to store data so that you can use the
binary search algorithm?
- How must the data be ordered in this structure?
- What is the time complexity for binary search?
- Is this time complexity guaranteed?
- What is the critical property of the data structure used
which allows the binary search algorithm to work?
- Can you use the binary search algorithm on a list? Explain
your answer in one sentence.
|
|
12 |
When using an array as a data structure:
- What are the advantages?
- What are the disadvantages?
|
|
13 |
Stacks
- List two ways that you could implement a stack.
- Which is the most common?
- Why is this method used, despite its limitations?
- A stack provides FIFO semantics. True or false?
|
|
14 |
For simple binary search tree, what is
time complexity for:
Operation |
Usual O() |
Guaranteed? Yes/No |
Worst case O() |
adding an item | | | |
deleting an item | | | |
searching for an item | | | |
What does the usual behaviour depend upon?
|
|
15 |
A binary tree has n nodes.
What are the bounds on the height of the tree?
|
|
16 |
- Why is a complete tree important?
- What is its critical property?
|
|
17 |
For a heap, what is the time complexity for:
Operation |
Usual O() |
Guaranteed? Yes/No |
Worst case O() |
Adding an item | | | |
Deleting an item | | | |
Searching for an item | | | |
|
|
18 |
Heaps
- What would be the
- minimum,
- maximum
number of elements in a heap of height h?
- Where in a heap would I find the smallest element?
- Is an array that is sorted in reverse order a heap?
- Is { 23, 17, 14, 6, 13, 10, 1, 5, 7, 12 } a heap?
|
|
19 |
Sorting time complexity: fill in the table.
Sort Algorithm |
Best O() |
Guaranteed? Yes/No |
Usual O() |
Worst case O() |
Conditions for best or worst case |
Bubble | | | | | |
Insertion | | | | | |
Heap | | | | | |
Quick | | | | | |
|
|
20 |
Sorting
- What is the space complexity of the "standard" recursive quicksort?
Hint: Consider the stack frames used.
- When would you prefer heap sort to quick sort?
- When would you prefer quick sort to heap sort?
- Suggest three situations in which bubble or insertion sort
might be used effectively.
Give a one sentence explanation in each case.
|
|
-
Algorithm A requires 200 machine cycles for each iteration and
requires nlogn iterations to solve a problem of size
n.
A simpler algorithm, B, requires 25 machine cycles for each iteration and
requires n2 iterations to solve a problem of size
n.
Under what conditions will you prefer algorithm A over algorithm B?
- Simple ADT Design
A double-ended queue or
deque
is one that has
both LIFO and FIFO behaviour,
ie you can add an item to the head or the tail of a list
and extract an item from the head or the tail.
Taking the following specification for the Collection class,
modify it to handle a deque.
Note:
- There are quite a few ways that a software engineer could do this:
see how many you can devise!
- A software engineer would probably try to ensure that code using the
original specification continued to function correctly.
- C++ and Java programmers should be able to produce versions
in those languages from this outline with little effort.
Similarly, modify the implementation to handle a deque.
/* Specification for Collection */
typedef struct t_Collection *Collection;
Collection ConsCollection( int max_items, int item_size );
/* Construct a new Collection
Pre-condition: max_items > 0
Post-condition: returns a pointer to an empty Collection
*/
void AddToCollection( Collection c, void *item );
/* Add an item to a Collection
Pre-condition: (c is a Collection created by a call to
ConsCollection) &&
(existing item count < max_items) &&
(item != NULL)
Post-condition: item has been added to c
*/
void DeleteFromCollection( Collection c, void *item );
/* Delete an item from a Collection
Pre-condition: (c is a Collection created by a call to
ConsCollection) &&
(existing item count >= 1) &&
(item != NULL)
Post-condition: item has been deleted from c
*/
void *FindInCollection( Collection c, void *key );
/* Find an item in a Collection
Pre-condition: c is a Collection created by a call to
ConsCollection
key != NULL
Post-condition: returns an item identified by key if one
exists, otherwise returns NULL
*/
/* Linked list implementation of a collection */
#include /* calloc */
#include /* NULL */
#include /* Needed for assertions */
#include "collection.h" /* import the specification */
extern void *ItemKey( void * );
struct t_node {
void *item;
struct t_node *next;
} node;
struct t_collection {
int size; /* Needed by FindInCollection */
struct t_node *node;
};
collection ConsCollection(int max_items, int item_size )
/* Construct a new collection
Pre-condition: (max_items > 0) && (item_size > 0)
Post-condition: returns a pointer to an empty
collection
*/
{
collection c;
/* Although redundant, this assertion should be
retained as it tests compliance with the formal
specification */
assert( max_items > 0 );
assert( item_size > 0 );
c = (collection)calloc( 1, sizeof(struct t_collection) );
c->node = (struct t_node *)0;
c->size = item_size;
return c;
}
void AddToCollection( collection c, void *item )
/* Add an item to a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(existing item count < max_items) &&
(item != NULL)
Post-condition: item has been added to c
*/
{
struct t_node *new;
assert( c != NULL );
assert( item != NULL );
/* Allocate space for a node for the new item */
new = (struct t_node *)malloc(sizeof(struct t_node));
/* Attach the item to the node */
new->item = item;
/* Make the existing list `hang' from this one */
new->next = c->node;
/* The new item is the new head of the list */
c->node = new;
assert( FindInCollection( c, ItemKey( item ) ) != NULL );
}
void DeleteFromCollection( collection c, void *item )
/* Delete an item from a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(existing item count >= 1) &&
(item != NULL)
Post-condition: item has been deleted from c
*/
{
struct t_node *node, *prev;
assert( c != NULL );
/* The requirement that the collection has at least
one item is expressed a little differently */
assert( c->node != NULL );
assert( item != NULL);
/* Select node at head of list */
prev = node = c->node;
/* Loop until we've reached the end of the list */
while( node != NULL )
{
if ( item == node->item )
{
/* Found the item to be deleted,
re-link the list around it */
if( node == c->node )
/* We're deleting the head */
c->node = node->next;
else
prev->next = node->next;
/* Free the node */
free( node );
break;
}
prev = node;
node = node->next;
}
}