0% found this document useful (0 votes)
54 views4 pages

I N N N (N N I O (N: n-1 + n-2 + n-3 ..+ N-K KN - (1+2+..+k) KN - K (k+1) /2 2kn - K 2-k/2 The Complexity Is O (K N)

The document contains examples of algorithms and data structures including: 1) A complexity analysis showing that the sum of squares is O(n^3) 2) Integrating a function and showing it is less than a constant 3) Explaining that the logarithm of a function with respect to a base b is O(log(f(n))) if b > 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

I N N N (N N I O (N: n-1 + n-2 + n-3 ..+ N-K KN - (1+2+..+k) KN - K (k+1) /2 2kn - K 2-k/2 The Complexity Is O (K N)

The document contains examples of algorithms and data structures including: 1) A complexity analysis showing that the sum of squares is O(n^3) 2) Integrating a function and showing it is less than a constant 3) Explaining that the logarithm of a function with respect to a base b is O(log(f(n))) if b > 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Homework 2

1/
n n
2
∑i =1 +2 + 3 .. .+n ≤n +n +.. .+n =n( n )=n ⇒ ∑ i 2 =O ( n3 )
2 2 2 2 2 2 2 2 3

i =1 i=1

2/
n n −ln( 2) n
1−( ln ( 2) n+ 1 ) e
∑ i/ 2 =∫ 2i i = ln2 ( 2)
i
=1
i =1 1

Therefore, the equation is always < 2


3/
log f (n) 1
We have log b f ( n )= = log f (n)
log b logb

1
With b > 1 -> logb is constant → logb f ( n ) is O ( logf ( n ) ) with b> 1

4/
n-1 + n-2 + n-3 …..+ n-k
= kn – [1+2+..+k]
= kn – k(k+1)/2
= 2kn – k^2- k/2
 The complexity is O(k*n)
5/
int findMinRec(int A[], int n)
{
if (n==1)
return A[0);
return min(A[n-1), findMinRec(A,n-1));
}

Get the array for which the minimum is to be found


Recursively find the minimum according to the following:
Recursively traverse the array from the end
Base case: If the remaining array is of length 1, return the only present element
i.e. arr[0]
if(n == 1)
return arr[0];
Recursive call: If the base case is not met, then call the function by passing the
array of one size less from the end, i.e. from arr[0] to arr[n-1].
Return statement: At each recursive call (except for the base case), return the
minimum of the last element of the current array (i.e. arr[n-1]) and the element
returned from the previous recursive call.
return min(arr[n-1], recursive_function(arr, n-1));
Print the returned element from the recursive function as the minimum element

7/
Output Value
Push(8) 8
Push(3) 3-8
Pop() 8
Push(2) 2-8
Push(5) 5-2-8
Pop() 2-8
Pop() 8
Push(9) 9-8
Push(1) 1-9-8
Pop() 9-8
Push(7) 7-9-8
Push(6) 6-7-9-8
Pop() 7-9-8
Pop() 9-8
Push(4) 4-9-8
Pop() 9-8
Pop() 8

8/
Output Value
insertFirst(3) 3
insertLast(8) 3-8
insertLast(9) 3-8-9
insertFirst(5) 5-3-8-9
removeFirst() 3-8-9
removeLast() 3-8
first() 3 3-8
insertLast(7) 3-8-7
removeFirst() 8-7
last() 7 8-7
removeLast() 8

9/
a/
class Ticket {
public:
        string      name;
        string      ID;
        unsigned int    birthyear;

    void assign(){
        name = "NO NAME";
        ID = "0000";
        birthyear = 0;
    }
b/
class Ticket {
public:
        string      new_name;
        string      new_ID;
        unsigned int    new_birthyear;

    void assign(){
        cin>>new_name;
        cin>>new_ID;
        cin>>new_birthyear;
    }

c/
void add (Ticket temp, queue<Ticket> &waiting_list){
   temp.name = "Nguyen Van A";
   temp.ID = "0010";
   temp.birthYear = 1990;

   waiting_list.push(temp);
}

d/
void Print (queue<Ticket> waiting_list){
Ticket back= waiting_list.back();
cout<<back.name<<endl;
cout<<back.ID<<endl;
cout<<back.birthyear<<endl;
}

You might also like