algorithm - Running time of a sort code -


sort(b) = 0 (n-1)     x = (i+1);     j = (i+2) n          if b[x] > b[j]            x = j;     if x != (i+1)        temp = b[i+1];        b[i+1] = b[x];        b[x] = temp; 

what running time t(n)? problem inner loop (for j = (i+2) n ) worst case scenario inner loop? , best case? think same because independent, want make sure.

the running time o(n^2).

each inner loops takes o(n-i) time, increasing values of i 0 n-1.

this gives time complexity of:

t(n) <= const*(n-0 + n-1 + n-2 + ... + n-(n-1))   =       = const*(1 + 2 + ... + n) = const*(n(n+1)/2) 

the last equation comes sum of arithmetic progression.

since n(n+1)/2 in o(n^2), time complexity.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -