subject

**c++ question**

suppose you have two vector of integers x and y, each of which have n randomly distributed but distinct
values. we want to merge x and y into a third vector z such that z has all the integers of x and y,
additionally z should not have any duplicate values. for this problem we are not concerned with ordering
in any of these vectors.
a. here is one algorithm. what is the big-o of this algorithm?
void merge1(const vector& x, const vector& y, vector& z) {
z. clear();
z. reserve(x. size() + y.;
for (int i = 0; i < x. size(); ++i)
z. push_back(x[i]);
for (int j = 0; j < y. size(); ++j) {
bool duplicate = false;
for (int i = 0; i < x. size(); ++i) {
if (y[j] == x[i]) {
duplicate = true;
break;
}
}
if (! duplicate)
z. push_back(y[j]);
}
}
b. here is another algorithm that uses a sorting function, assume that the sort function is implemented as
quicksort. what is this algorithm’s big-o?
void merge2(const vector& x, const vector& y, vector& z) {
z. clear();
z. reserve(x. size() + y.;
for (int i = 0; i < x. size(); i++)
z. push_back(x[i]);
for (int j = 0; j < y. size(); j++)
z. push_back(y[j]);
sort(z. z.;
int last = 0;
for (int k = 1; k < z. size(); k++) {
if (z[last] ! = z[k]) {
last++;
z[last] = z[k];
}
}z
.resize(last + 1);
}
c. which algorithm performs better given the provided description of inputs?
d. suppose the input vectors are:
vector x{1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15,16,17,18,19,20};
vector y{21,22,23,24,25,26,27,28,29,30,31, 32,33,34,35,36,37,38,39};
how will that change your analysis done in the previous parts?

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 03:40, plzhelpmeasap46
Hello my name is mihai and i need your : )i have to do a python project in computer science and i’m really busy with my mocks this period of time besides this i’m not good at coding so could someone pls pls pls sort me out ? i actually beg ; ))
Answers: 1
image
Computers and Technology, 22.06.2019 08:30, marialuizavalen
Today is the anniversary of me being on yet, i don't need it anymore! here's a picture of my dog wearing a bowtie! my question is, how do i delete my account?
Answers: 1
image
Computers and Technology, 22.06.2019 10:20, alcantar28eduin
Print "usernum1 is negative." if usernum1 is less than 0. end with newline. convert usernum2 to 0 if usernum2 is greater than 10. otherwise, print "usernum2 is less than or equal to 10.". end with newline
Answers: 3
image
Computers and Technology, 23.06.2019 14:00, shawn423
How are stop motion special effects in animated films created
Answers: 1
You know the right answer?
**c++ question**

suppose you have two vector of integers x and y, each of which have n...

Questions in other subjects:

Konu
Mathematics, 24.08.2019 12:50