Question

Write a while loop whose time is θ(log n).

  1. Write a while loop whose time is θ(log n).

Homework Answers

Answer #1

Most classical example of a while loop whose time complexity is Logn is iterative approach of Binary Search.

Have a look at the below Python Code...

def binary_search(arr,n,low,high,x):

  low = 0

  high = n-1 

  while low<=high:

    mid = low + (high-low)//2 

    if arr[mid]==x:

      return mid 
    
    elif arr[mid]<x:
      low = mid + 1 

    else:

      high = mid - 1

The time complexity of above code is O(logn). If parameter of any loop gets divided or multiplied by a constant number then the time complexity of that loop will be logn .

And we all know that time complexity of binary search is logn.

Happy Learning!

Know the answer?
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
Similar Questions
Write a python while loop that sums all the numbers from m to n, where “m”...
Write a python while loop that sums all the numbers from m to n, where “m” and “n” are both user given values.
Write a program in C that calculates: n! = n*(n-1)*(n-2) ...3 ...2 ...1 Use for loop...
Write a program in C that calculates: n! = n*(n-1)*(n-2) ...3 ...2 ...1 Use for loop and while loop
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display...
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display 5 lines of smiley faces. The following would be the output of your program: Note: Sample program is in the "Important Documents", Module 6 folder) Hello:                          Bye!
Write a while loop that prints to the screen: ONLY the multiples of 3 which are...
Write a while loop that prints to the screen: ONLY the multiples of 3 which are between 4 and 40. In C code please.
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
Write a function using while loop, add the number to 10, then return the product to...
Write a function using while loop, add the number to 10, then return the product to the main function. Use double.
Given the recurrence equation below, find running time T(n). T(n) = 8T(n/2) + n3 log n4...
Given the recurrence equation below, find running time T(n). T(n) = 8T(n/2) + n3 log n4 T(n) = 2T(n1/2) + log n
Write a Python program using while loop that finds and prints the sum of this geometric...
Write a Python program using while loop that finds and prints the sum of this geometric series. 1 + 2 +4 +8 + … + 1024
write a program using a while loop that asks a user to enter scores of exam...
write a program using a while loop that asks a user to enter scores of exam for each subject and calculate the total and average. in c++
Write a program with while loop that prints all numbers between 5 and 100 (inclusive) that...
Write a program with while loop that prints all numbers between 5 and 100 (inclusive) that are divisible by 5.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT