Practice Set Loop Complexities
Practice Set Loop Complexities
.
.
For i = 3# = 𝑛, inner loop will run log 3# times (here k = log 𝑛)
(We will consider outermost for loop after a while, lets focus on inner for loop)
Inner for loop will run log1 + log3 + log 3! + log 3" + …..+ log 3#
= log 1.3.3! . 3" . 3 … .3# = log 3$%!%"%⋯#
!
= log 3# = 𝑘 ! = 𝑙𝑜𝑔𝑛 !
Question 6
.
.
For i =n, inner loop will run n/(2xn) times
Time Complexity :
( ( ( ( ( (
= ! + !×! + !×" + !×+ + !×, + … … . . + !×( =
= 𝑛/2 [1/2 + 1/3 + 1/4 + ⋯ . . 1/𝑛] = 𝑛𝑙𝑜𝑔𝑛
Question 10
What will be the time complexity of following loop ?
Answer: 𝜃(𝑙𝑜𝑔𝑛)
Question 11
.
.
For j =n, inner loop will run n/n =1 time
Time Complexity :
( ( ( ( ( (
= $ + ! + " + + + , + ……..+(
= 𝑛 [1 + 1/2 + 1/3 + 1/4 + ⋯ . . 1/𝑛] = 𝑛𝑙𝑜𝑔𝑛
Question 12
.
.
For i = 2# = 𝑛, inner loop will run 2# times (here k = log 𝑛)
Question 14
https://courses.cs.washington.edu/courses/cse332/21au/exams/oldExams/cse332-midterm-12wi-soln.pdf
Answer: 𝜃(𝑛)
Question 18 What will be the time complexity of following nested loop ?
Answer: 𝜃(𝑛" )
Question 19