Question

Please compute the time complexity of the algorithm. (ex: O(N) or O(N log N))

void Set::unite(const Set& seti, const Set& set2) { const Set* sp = &set2; if (this == &seti) { if (this == &set2) return; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: O(N)

The first loop runs for N times since it runs till all elements of sp has been traversed and sp will only contain either set1 or set2 (from if else statements at top) and both have N elements.

The second loop also runs till all elements of sp has been traversed i.e. N times.

Thus complexity = O(N+N) = O(N)

Add a comment
Answer #2

The time complexity for the above code is O(N). The reason for the same is both the loops run N times only.

answered by: Shivani Sharma
Add a comment
Know the answer?
Add Answer to:
Please compute the time complexity of the algorithm. (ex: O(N) or O(N log N)) void Set::unite(const...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • We know that binary search on a sorted array of size n takes O(log n) time....

    We know that binary search on a sorted array of size n takes O(log n) time. Design a similar divide-and-conquer algorithm for searching in a sorted singly linked list of size n. Describe the steps of your algorithm in plain English. Write a recurrence equation for the runtime complexity. Solve the equation by the master theorem.

  • 1. Time Complexity of Kruskal's Algorithm Which best describes the relative time complexities of the pre-sorting...

    1. Time Complexity of Kruskal's Algorithm Which best describes the relative time complexities of the pre-sorting and main parts of algorithm? A) The time to pre-sort dominates B) The main part dominates C) The relationship depends on the sort and disjoint-set operations being used D) Kruskal's algorithm doesn't use pre-sorting 2. Kruskal's Algorithm: Disjoint Set Operations What are the number of calls to the respective disjoint set operations in Kruskal's Algorithm? A) MAKE-SET O(V), FIND O(V), UNION (V) B) MAKE-SET...

  • What is the time complexity of this code? I'm unsure if it is O(log(n)) or O(n)....

    What is the time complexity of this code? I'm unsure if it is O(log(n)) or O(n). I think that the while loop is logn but the for loop that comes after runs the same number of times as the while loop. string toBinary(int num) { string binary = "", temp = ""; while (num != 0) { temp += to_string(num%2); num /= 2; for (int i = temp.size() - 1; i >= 0; i--) { binary += temp[i]; return binary;

  • Please Help ASAP. 1Consider the below code which iterates over a linked list of n nodes...

    Please Help ASAP. 1Consider the below code which iterates over a linked list of n nodes (assume the list has at least 1 node). How many lines of output will it write? Node *thisNode = headPtr; while (thisNode != null) { cout << thisNode->item << endl; thisNode = thisNode->next; } 1.n 2.1 3.n2 4.n / 2 5.2 * n 2The below algorithm contains nested loops.   for (int total = 1; total <= n; total++) { for (int samples = 0;...

  • Therefore, getx-log,n from 2on. So the time complexity of this loop is O logn). Eliminate low...

    Therefore, getx-log,n from 2on. So the time complexity of this loop is O logn). Eliminate low order terms 26 00:01 00:01 Homework #7 Homework #7 - 7.1 Design the algorithm of Towers of Hanoi. . Source peg, Destination peg, Auxlary peg s k disks on the source peg, a bigger disk can never be on top of a smaler dsk : Need to move al k disks to the destination peg using the auxillary peg, without ever keeping a bigger...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The...

    JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The best-case performance for a shell sort is: --- O(1) O(n2)   O(n) O(n log n)   Signaler cette question Question 22 points The best-case performance for an array of n items using insertion sort is: --- O(n2)   O(n) O(1) there is no best-case Signaler cette question Question 3 2 points A recursive method that processes a chain of linked nodes --- uses the first node in...

  • HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE...

    HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE PROGRAM RUNS. Task 1: Enforcing const-ness throughout Your first job will be to go through all of the code and decide which functions should be declared const. You should find several places throughout the program where this makes sense. We will also make the id data member in the Customer class const , as once a customer has been created their ID will never...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT