ECE391: Computer System Engineering Homework 2: 1. Show That
ECE391: Computer System Engineering Homework 2: 1. Show That
ECE391: Computer System Engineering Homework 2: 1. Show That
𝑛 𝑛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
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];
}
#include <iostream>
using namespace std;
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;
if (size == 1) {
printArr(a, n);
return;
}
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
Ticket()
{
name="NO NAME";
ID="0000";
birthyear=0;
};
b). Write constructor for the class Ticket to assign the new information
into the member variables.
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;
};
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;
d). Write a function to get the information of a person at the front of the
Waiting_list.
void getinfo(queue<Ticket> *Waiting_list)
{
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;