Question
Asked Dec 2, 2019
12 views

What is the Big-Oh Runtime (in terms of n) for the following segment of code?

 

int i;

int used[n];

for (i = 0; i < n; i++) {

    used[i] = 0;

}

for (i = 0; i < n; i++) {

    if (used[i] == 0) {

        used[i] = 1;

        i = -1;

     }

}

 

 

check_circle

Expert Answer

Step 1

Generally, we define the complexity of any code by using Big O- Notation. The complexity depends upon the kind of statements used in the code.

1)Sequence of statements-

statement 1;

statement 2;

  ...

statement k;

The total time is determined by adding the times for all statements:

total time = time(statement 1) + time(statement 2) + ... + time(statement k)

If each statement  involves basic operations, then the time for each statement is constant and the total time is also constant: O(1).

2) if-then-else statements

if (condition) {

    sequence of statements 1

}

else {

    sequence of statements 2

}

In this case, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case time is the slowest of the two possibilities: max(time(sequence 1), time(sequence 2)). For example, if sequence 1 is O(N) and sequence 2 is O(1) the worst-case time for the whole if-then-else statement would be O(N).

3) for loops

for (i = 0; i < N; i++) {

 &nb...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Simplify the following Boolean function and expression, using four-variable maps  b) F = x’z + w’xy’...

A: Click to see the answer

question_answer

Q: Simplify the following Boolean function and expression, using four-variable maps  a) cF(w,x,y,z) = Σ...

A: Since you have posted multiple parts and have not specified which part needs to be answered. So, we ...

question_answer

Q: In C++: Examine the code segment. What will be printed? If there are any errors indicate the nature ...

A: The code given in question will give two compile time errors. Both the errors are indicated in bold ...

question_answer

Q: Python Coding-While Loops  (In PyCharm Community edition)   Create a while loop that SUMS up ALL the...

A: Prompt user to enter initial numberPrompt user to enter ending numberUsing while loop sums up all th...

question_answer

Q: 80/20 rule states that 80% of the instruction ios executed and 20% of the instruction is generated i...

A: This rule is discovered by Wilfredo pareto and it is also called as pareto principle. it is a very u...

question_answer

Q: In C++ Language (please use hint) : Write a function which will swap its arguments if the first argu...

A: Call by reference: In this approach, passing the arguments to a method copies the address or referen...

question_answer

Q: In C++: The members of the board of a small university are considering voting for a pay increase for...

A: A C++ program that will prompt for and accept the current salary for each of the faculty members, th...

question_answer

Q: How can I create the following table using printf() method?

A: Program algorithm-STARTStep1: Define the class tableStep2: Define the main method.Step3: Use format ...

question_answer

Q: Transient PopulationPopulations are affected by the birth and death rate, as well as the number of p...

A: Program:#include &lt;iostream&gt;using namespace std;double population(double P, double B, double D,...