Question

In: Computer Science

What is the time complexity of my algorithm? It uses an vector E that contains and...

What is the time complexity of my algorithm? It uses an vector E that contains and object made of a string and an integer. It takes an empty vector as parameter and returns the vector with the top three integers from the objects in E .

void Trendtracker::top_three_trends(vector<string> &T) {
   T.clear();
   if (E.size() == 0) {
       return;
   }

   if (E.size() == 1) {
       T.push_back(E[0].hashtag);
   }

   else if (E.size() == 2) {
       if (E[0].pop > E[1].pop) {
           T.push_back(E[0].hashtag);
           T.push_back(E[1].hashtag);
       }
       else {
           T.push_back(E[1].hashtag);
           T.push_back(E[0].hashtag);
       }
   }
   else {
       vector<Entry> temp;
       temp = E;

       for (int i = 0; i < 3; i++) {
           int topPop = temp[0].pop;
           string topHashtag = temp[0].hashtag;
           int index = 0;
           for (int j = 1; j < temp.size(); j++) {
               if (temp[j].pop > topPop) {
                   topPop = temp[j].pop;
                   topHashtag = temp[j].hashtag;
                   index = j;
               }
           }
           T.push_back(topHashtag);
           temp.erase(temp.begin() + index);
      
       }
   }

}

Solutions

Expert Solution

void Trendtracker::top_three_trends(vector<string> &T) {
T.clear();//O(1)
if (E.size() == 0) //O(1)
{
return;//O(1)
}

if (E.size() == 1) //O(1)
{
T.push_back(E[0].hashtag);//O(1)
}

else if (E.size() == 2) //O(1)
{
if (E[0].pop > E[1].pop) //O(1)
   {
T.push_back(E[0].hashtag);//O(1)
T.push_back(E[1].hashtag);//O(1)
}
else
   {
T.push_back(E[1].hashtag);//O(1)
T.push_back(E[0].hashtag);//O(1)
}
}
else {
vector<Entry> temp;//O(1)
temp = E;//O(1)

for (int i = 0; i < 3; i++) //O(3) =O(1)
   {
int topPop = temp[0].pop;//O(1)
string topHashtag = temp[0].hashtag;//O(1)
int index = 0;//O(1)
for (int j = 1; j < temp.size(); j++)//O(n) //n is size of temp
       {
if (temp[j].pop > topPop)//O(1) //runs n times
           {
topPop = temp[j].pop;//O(1)
topHashtag = temp[j].hashtag;//O(1)
index = j;//O(1)
}
}
T.push_back(topHashtag);//O(1)
temp.erase(temp.begin() + index);//O(1)
  
}
}

}

Total complexity : O(n)//worst case
best case :O(1)


Related Solutions

What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity of O(n^2)? Justify.
What is the Average time complexity of sequential search algorithm in a linked list?
What is the Average time complexity of sequential search algorithm in a linked list?
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons...
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons between elements is the following: Ωn log n in the worst case. you need to prove this , if you can please type answer.
what is the time complexity of the edit distance ("ALASKA" vs "ALABAMA" example) dynamic programming algorithm?...
what is the time complexity of the edit distance ("ALASKA" vs "ALABAMA" example) dynamic programming algorithm? what is the space complexity of the edit distance ("ALASKA" vs "ALABAMA" example) dynamic programming algorithm?
1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm....
1. Mathematically analyze the given Recurrence Relation and find out the time complexity of the algorithm. T(n) = T(n-1)+1 , if n> 0 1 if n = 0
Calculate the time complexity of each algorithm for best, worst and average cases. Give all steps...
Calculate the time complexity of each algorithm for best, worst and average cases. Give all steps of your calculation and results in Big-O notation. algorithm : Selection Sort Bubble Sort Insertion Sort
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
i) Pen down the algorithm for Radix sort. ii) What is the complexity of Radix sort?...
i) Pen down the algorithm for Radix sort. ii) What is the complexity of Radix sort? iii) Give any 2 real time examples of Radix sort.
Please explain what we mean by Time Complexity and Space Complexity. Please provide a short survey...
Please explain what we mean by Time Complexity and Space Complexity. Please provide a short survey of Complexity classes.
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in...
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in it, determines whether G has a cycle containing e. Your algorithm should also return the length (number of edges) of the shortest cycle containing e, if one exists. Just give the algorithm, no proofs are necessary. Hint: you can use BFS to solve this.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT