Assignment I
Assignment I
1. Write a C++ program that displays the total running time of a given algorithm based on
different situations such as processor speed, input size, processor load, and software
environment (DOS and Windows).
2. Calculate the T(n) with big O for the following fragment of codes: (Warning: Please
show necessary steps).
For example:
for (int i=1;ji<=n;i++)
cout<<i;
T(n)=1+n+1+n+n
= 3n+2
T(n)=O(n). This indicates as T(n) is big O (or O(n))
a. int sum=0, n, m;
for(int=1;i<=n;i++)
for(int j=1;j<=m;j++)
Sum++;
b. int sum=0, i, j, n;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
sum++;
INSTRUCTOR: MELKAMU A. 1
c.
for (int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
Cout<<i;
d.
for(int i=1;i<=n;i++)
int Sum=sum+i;
for (i=1;i<=n;i++)
for(j=1;j<=m;j++)
Sum++;
e.
if(k==1)
{
for (i=1;i<=100;i++)
for (j=1;j<=1000;j++)
Cout<<I
}
else if(k==2)
{
for(i=1;i<=n;i=i*2)
Cout<<i;
}
else
{
{for (i=1;i<=n;i++)
Sum++;
}
INSTRUCTOR: MELKAMU A. 2
f.
for (i=1; i<=n;i++)
{
If (i<10)
for(j=1;j<=n;j=j*2)
cout<<j;
Else
cout<<i;
}
g.
INSTRUCTOR: MELKAMU A. 3
Part II-Searching and Sorting Algorithms
3. Write a C++ program that will implement all of the sorting algorithms. The program
should accept different unsorted data items from user and sort using all algorithms and
should tell which algorithm is efficient for that particular unsorted data items. (Hint: use
how many comparison and swaps are done so the one with the less number of swap and
comparison number is considered as an efficient algorithm for that particular data items.)
Notice
INSTRUCTOR: MELKAMU A. 4