ECE391: Computer System Engineering Homework 2: 1. Show That

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

ECE391: Computer System Engineering


Homework 2
n
1. Show that i
i 1
2
is O (n 3 )

𝑛 𝑛3 −1
We have ∑𝑛𝑖=1 𝑖 2 < ∫1 𝑖 2 𝑑𝑖 = < 𝑛3
3

⇒ ∑𝑛𝑖=1 𝑖 2 is O(𝑛3 )
n
2. Show that i / 2
i 1
i
2

𝑖 1 2 3 4
Let 𝑆 = ∑∞
𝑖=1 = + + + +⋯+0 (1)
2𝑖 2 4 8 16

𝑆 1 2 3
⇒ = + + + ⋯ + 0 (2)
2 4 8 16
Subtracting (1) by (2), we get

𝑆 1 1 1 1
= + + + + ⋯+ 0 = 1
2 2 4 8 16
⇒𝑆=2
𝑖 𝑖
We have ∑𝑛𝑖=1 < ∑∞
𝑖=1 =𝑆=2
2𝑖 2𝑖

𝑛
𝑖
⇒∑ <2
2𝑖
𝑖=1

3. Show that logb f (n) is (log f (n)) if b  1 is a constant


Find c and c’ satisfy
𝑐 log 𝑓(𝑛) ≤ log 𝑏 𝑓(𝑛) ≤ 𝑐′ log 𝑓(𝑛)
𝑐 ≤ log 𝑏 10
⇔{ ′
𝑐 ≥ log 𝑏 10

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

Since we can find c and c’ satisfy 𝑐 log 𝑓(𝑛) ≤ log 𝑏 𝑓(𝑛) ≤ 𝑐′ log 𝑓(𝑛),
log 𝑏 𝑓(𝑛) is Θ(log 𝑓(𝑛))
4. Find the complexity of the function
int selectkth(int a[], int k, int n) {
int i, j, mini, tmp;
for (i = 0; i < k; i++) {
mini = i;
for (j = i+1; j < n; j++)
If (a[j]<a[mini])
mini = j;
tmp = a[i];
a[i] = a[mini];
a[mini] = tmp;
}
return a[k-1];
}

Outer loop is doing k iterations.


Inter loop is doing n-1 iterations but it does that k times.
So we have O(k*(n-1)) = O(kn-k) Since k can be equal to n (we can ask for n-
th smallest integer in an array) the expression becames O(n*n-n) = O(n2-n)
= O(n2).

5. Describe a linearly recursive algorithm for finding the minimum element


in an n-element array.

#include <iostream>
using namespace std;

int findMinRec(int A[], int n) //n is number of elements of A


{
if (n == 1)

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

return A[0];
return min(A[n-1], findMinRec(A, n-1));
}
6. Describe a recursive algorithm for printing all of the permutations of the
sequence (1,2,…,n). What is the running time of your algorithm?
#include <iostream>
using namespace std;

// Prints the array


void printArr(int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
printf("\n");
}

// Generating permutation using Heap Algorithm


void heapPermutation(int a[], int size, int n)
{

if (size == 1) {
printArr(a, n);
return;
}

for (int i = 0; i < size; i++) {


heapPermutation(a, size - 1, n);

// if size is odd, swap 0th i.e (first) and


// (size-1)th i.e (last) element
if (size % 2 == 1)
swap(a[0], a[size - 1]);

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

// If size is even, swap ith and


// (size-1)th i.e (last) element
else
swap(a[i], a[size - 1]);
}
}
7. Describe the output of the following series of stack operations:
push(8), push(3), pop(), push(2), push(5), pop(), pop(), push(9), push(1),
pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop().
Operation Output Stack
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. Describe the output for the following sequence of queue operations:


insertFirst(3), insertLast(8), insertLast(9), insertFirst(5), removeFirst(),
removeLast(), first(), insertLast(7), removeFirst(), last(), removeLast().
Operation Output Queue
insertFirst(3) 3

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

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. Given a class Ticket below


Class Ticket {
public:
string name;
string ID;
unsigned int birthyear;
}
a). Write a constructor for the class Ticket to assign the default values for
name, ID, and birthyear.

Name = “NO NAME”;


ID = “0000”;
Birthyear = 0;

Ticket()
{
name="NO NAME";
ID="0000";
birthyear=0;
};

b). Write constructor for the class Ticket to assign the new information
into the member variables.

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

string new_name;
string new_ID;
unsigned int new_birthyear;
Ticket()
{
cout<<endl<<"Name: ";
getline(cin, name);
cout<<"ID = ";
cin>>ID;
cout<<"Birthyear = ";
cin>>birthyear;

};

Given a QUEUE of Ticket named Waiting_list as below:


queue<Ticket> Waiting_list;

c). Write a function to add a person with the following information into
the Waiting list:
string my_name = “Nguyen Van A”;
string my_ID= “0010”;
unsigned int my_birthyear = 1990;

void enqueue(queue<Ticket>* Waiting_list)


{
Ticket t;
Waiting_list->push(t);
}

d). Write a function to get the information of a person at the front of the
Waiting_list.
void getinfo(queue<Ticket> *Waiting_list)

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

{
cout<<endl<<"At the front of the waiting list: "<<endl;
cout<<"Name: "<<(Waiting_list->front()).name<<endl;
cout<<"ID = "<<(Waiting_list->front()).ID<<endl;
cout<<"Birthyear: "<<(Waiting_list->front()).birthyear<<endl;

Instructor: Dr. Truong Quang Vinh

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy