0% found this document useful (0 votes)
47 views

Data Structure Worksheet

Uploaded by

Dutrei Hailu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Data Structure Worksheet

Uploaded by

Dutrei Hailu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Data Structures and Algorithms

Worksheet
Exercise 1
1. Define the following terms
a. Algorithm
b. Data structures
c. Complexity Analysis
d. Big-Oh Notation

2. Give a Big-Oh , in terms of n, of the running time of each of the following methods.

a. int ExA( int n ) d. int ExD( int n )


{ int a; { int a;
for ( int i = 0; i < n; i += 2 ) for ( int i = 0; i < n * n; i++ )
a = i; for ( int j = 0; j <= n; j++ )
} a = i;
}
b. int ExB( int n )
{ int a; e. int ExE( int n )
for ( int i = 0; i < n * n; i++ ) { int a;
a = i; for ( int i = 0; i < n; i++ )
} for ( int j = 0; j <= n - i; j++ )
a = i;
}
c. int ExC( int n ) {
int a; f. int ExF( int n )
for ( int i = 0; i < n; i++ ) { int a;
for ( int j = 0; j <= i; j++ ) for ( int i = 1; i < n; i *= 2 )
a = i; a = i;
} }

3. Put the followings in order of their complexity

a. 6n log n + 2n e. 1 / n i. n! [That's n factorial.]


b. ( 1 / 3 )n f. 210 j.
2log n
c. n n g. n - n 3 + 7n 5 k. 2n
d. log log n h. n / log n l. 3n + 100 log n

Exercise 2
1) For each of the following two
loops:
 Write an expression f(n) that defines the number of assignment operations executed
by the code
 State what the big-O complexity of the code is
 Using definition 3, suggest reasonable values for c and N

1
i. for (cnt1=0, i=1; i<=n; i++)
for (j=1; j<=n; j++)
cnt1++;

ii. for (cnt2=0, i=1; i<=n; i++)


for (j=1; j<=i; j++)
cnt2++;

2) For each of the following two loops, state what the big-O complexity of the code is:

i. for (cnt3=0, i=1; i<n; i*=2)


for (j=1; j<=n; j++)
cnt3++;

ii. for (cnt4=0, i=1; i<=n; i*=2)


for (j=1; j<=i; j++)
cnt4++;

3) The following conversation was overheard between a programmer and a computer scientist:
Programmer: I just wrote a really fast program to solve the WOW problem. I wrote it in
assembler, optimized every loop, and used every special instruction available on the Quadrium-7
processor.
Computer Scientist: How fast is it?
Programmer: Well, for 10 BogoUnits, it took 10 seconds to find the optimum, and for 11, it took
20 seconds, and for 12, it took 40 seconds, and for 13, it took 80 seconds, and so on.
Computer Scientist: But that's really slow! I have a O( n3 ) algorithm, which is much faster.
Programmer: Faster? I tried your program and it took 1000 seconds for 10 BogoUnits. How
can you claim your method is faster?
a. Estimate how long it would take the Computer Scientist's algorithm to solve the problem
for 20 BogoUnits.
b. Estimate how long it would take the Programmer's program to solve the problem for 20
BogoUnits.
c. What additional information do you need to decide which solution is in fact better for the
actual problem?

You might also like