Professional Documents
Culture Documents
Lab
Lab
MSV: 21021618
Ex1:
class Complex
};
Ex2:
void findSmallestOddNumber()
int MinOdd=9999;
for(temp=head;temp!=NULL;temp=temp->next)
if(temp->data % 2 != 0 )
if(MinOdd>temp->data)
MinOdd=temp->data;
if(head->data %2 != 0 )
head = head->next;
delete temp;
for(;head!=NULL;head=head->next)
else if(head->next->data % 2 != 0 )
deleteNode(head);
Ex3:
Function 2logn can reverse to nlog2 and has the big O notation is: O(n)
nlogn 1 n
n n 2n
N2 N3 nlogn
1<n<nlogn<n2<n3<2n
EX4:
Algorithm 1:
int res = 1;
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