You are on page 1of 6

Name: Vương Hoàng Minh

MSV: 21021618

Ex1:

class Complex

float a,b; // a+bi – a is real, b is imaginary

};

Ex2:

void findSmallestOddNumber()

{ Node* temp = head;

int MinOdd=9999;

for(temp=head;temp!=NULL;temp=temp->next)

if(temp->data % 2 != 0 )

if(MinOdd>temp->data)

MinOdd=temp->data;

void removeOddNumber(Node* head)

if(head->data %2 != 0 )

Node* temp= head;

head = head->next;
delete temp;

for(;head!=NULL;head=head->next)

else if(head->next->data % 2 != 0 )

deleteNode(head);

Ex3:

Function 4nlogn + 2n has the big O notation is: O(nlogn + n) = O(nlogn),

Function 210 has the big O notation is: O(210) = O(1),

Function 2logn can reverse to nlog2 and has the big O notation is: O(n)

Function 3n+100logn has the big O notation is O(n+logn) = O(n)

Function 4n has the big O notation is O(4n) = O(n)

Function 2n has the big O notation is O(2n)

Function n2 + 10n has the big O notation is O(n2)

Function n3 has the big O notation is O(n3)

Function nlogn has the big O notation is O(nlogn)

nlogn 1 n
n n 2n
N2 N3 nlogn

Increasing order of Big O notation:

1<n<nlogn<n2<n3<2n

210<2logn<3n+100logn<4n<4nlogn + 2n <nlogn <n2+10n<n3<2n

EX4:
Algorithm 1:

int res = 1;

for(int i = 1; i<=n; i++)

res*=2;

T(n) = O(n)

Algorithm 2:

int power2(int n)

If(n==0)

Return 1;

Return 2*power2(n-1);

T(n) = O(logn)

Ex5:
sum  = 0;(1)
for ( i = 0; i < n; i + +) (2)
                        for ( j = i + 1; j < = n; j + +) (3)
                                     for ( k = 1; k < 10; k + +) (4)
                                                    sum = sum + i * j * k; (5)
1,T(n) = 1

2, t(n) = n

3, t(n) = n

4, T(n) = n

5, t(n) = 1

 T(n) = 1 + n*n*n*1 = O(n3)


for (i = 0 ; i < n ;  i++)
for (j = 0 ; j < n ; j++)
if (i == j)
A[i][j] = 1;
else
A[i][j] = 0;
T(n) = n*n*(1+1) = O(n2)

You might also like