Lab exercise 0 - Time Complexity

advertisement
Algoritmer og Datastrukturer 1
Lab Exercise 0 – Time Complexity
14.01.2008/TFJ
F2008
Lab Exercise 1 – Time Complexity
This exercise will train you in reading pseudo code, defining pre- and postconditions and determining time
complexities.
A Vector is, generally speaking, an ordered sequence of numbers. Below is the definition of a class Vector
and pseudo code for the implementation of the class’ methods.
VECTOR.PSEUDOCDE
VECTOR.H
class Vector
{
public:
static const int SIZE = 10 ;
Vector();
void set(int index, int number);
int get(int index);
bool contains(int number);
int howMany(int number);
bool allUnique();
private:
int data[SIZE];
};
Vector::Vector()
{
for all i in [0..SIZE[
data[i] = 0
}
void Vector::set(int index, int number)
{
data[index] = number;
}
int Vector::get(int index)
{
return data[index]
}
bool Vector::contains(int number)
{
for all i in [0..SIZE[
if data[i] = ‘number’ return true
return false
}
int Vector::howMany(int number)
{
for all i in [0..SIZE[
if data[i] = ‘number’ ++retVal
return retVal
}
bool Vector::allUnique()
{
for all i in [0..SIZE-1[
for all j in i ]i+1..SIZE[
if data[i] = data[j] return false;
return true;
};
Exercise 1:
Explain in loose terms what each method does.
Exercise 2:
Define suitable pre- and postconditions for each method.
Exercise 3
Find the time complexity for each of the methods.
Exercise 4:
Implement and test the class Vector. Remember to include your pre- and postconditions.
Page 1 of 1
Download