Time complexity of nested while loop with if-else -


i getting confused how analysis time complexity within nested while loop divide odd , situation. explain how deal situation?

     = 1       while (i < n) {         k =         while ( k < n ) {          if ( k % 2 == 1 )               k ++           else               k = k + 0.01*n        }        = + 0.1*n      } 

so in problem this, factors 0.01 , 0.1 play huge role.

first let's consider inner while loop, if k odd, increment k 1. if k even, increment k one-hundredths of n. how man iterations can inner while loop run?

clearly if iterations of type-1(odd case), inner while loop run n-k times, , if iterations of type-2(even case), inner while loop run atmost 100 times(as increment value of k one-hundredths of n each time).

given value of k, number of iterations of inner while loop is: max(n-k,100). on, assume value of n-k greater 100 always, without loss of generality.

okay, how outer loop iterate? in each iteration of outer loop, value of increases one-tenths of n each time, outer while run @ 10 times.

making running times explicit , calculating overall running time:

running time first iteration of outer loop  :   n-k  running time second iteration of outer loop : + n-(k+0.1*n)                                                    + n-(k+0.2*n)                                                    ...                                                    + n-(k+0.9*n)                                                   -----------                                                 = 10n-10k-(4.5)n 

plugging in k=1(as start value of k), 10n-10-4.5n = 5.5 n -10 = o(n)

hence complexity o(n) time.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -