What would the Big O designation mean for two loops that are not nested?
Example:
for(int i=0; i<n; i++){ System.out.println(i); } for(int j=0; j<n; j++){ System.out.println(j); }
Linear
O(n) + O(n) = 2*O(n) = O(n)
It does not matter how many nested loops you have (if this number is a constant and does not depend on n ), the complexity will be linear and will be equal to the maximum number of iterations in the loop.
n
Technically, this algorithm still works in O (n) time.
While the number of iterations increases by 2 for each increase in n , the time, which is still increasing, increases with the speed linear , therefore, in O (n) time.
It will be O(n) + O(n) ==> Effectively O(n) , since we do not store constant values.
O(n)
This will be O (2n) because you are doing n + n = 2n iterations.
O (2n) is essentially equivalent to O (n) since 2 is a constant.
Source: https://habr.com/ru/post/1239008/More articles:Cannot access TCP server in universal Windows application - win-universal-appUnderstanding the sizeof operator in C - cTabLayout with ViewPager does not work inside Android fragment - androidApache Installation Request - apacheHow to enable "gx" in Vim? Mine doesn't work anymore - vimDynamic Height UITableView - iosRealm = RLMRealm 'has no member setDefaultRealmPath' - iosHow to access an array in php by index? - phpHow to use applicationData in PKPaymentRequest (ApplePay)? - iosJQuery fades out one id at a time - jqueryAll Articles