What is the complexity of the cycle that goes through this
for (int i = 0; i < n; i++) { for (int j = 0; j < log(i); j++) { // do something } }
In my opinion, the inner loop will work log(1)+log(2)+log(3)+...+log(n)once, so how can I calculate its complexity?
log(1)+log(2)+log(3)+...+log(n)
So you have the amount log(1) + log(2) + log(3) + ... + log(n) = log(n!). Using the Stirling approximation and the fact that ln(x) = log(x) / log(e)one can obtain
log(1) + log(2) + log(3) + ... + log(n) = log(n!)
ln(x) = log(x) / log(e)
log(n!) = log(e) * ln(n!) = log(e) (n ln(n) - n + O(ln(n)))
which gives the same complexity O(n ln(n))as the other answer (with a slightly better understanding of related constants).
O(n ln(n))
, "" . do_something, O(1) log N, log N. O(N log N). , .
do_something
O(1)
log N
O(N log N)
: , " -" O(1) ( N, , ).
N
log(1)+log(2)+log(3)+...+log(n). log(n/2) = log(n) - log(2). , n / 2 * (log(n) - log(2)) = Omega(nlog(n)). , n , log(n), O(nlog(n)).
log(n/2) = log(n) - log(2)
n / 2 * (log(n) - log(2)) = Omega(nlog(n))
n
log(n)
O(nlog(n))
Source: https://habr.com/ru/post/1650657/More articles:Django получает все значения. Группа. В частности, одно поле - djangoОшибка преобразования строки в Laravel 4.2 - phpValueError: all input arrays must have the same number of dimensions - pythonDefine a udf spark by reflection on String - scalaHow to count from two fields in mongoDB - mongodbSerializing BigIntegerField, TextField in a Django serializer - djangoIs there a way to add a description of AWS Security Group rule rules - amazon-web-servicesHow to pass a string to an Angular 2 function (click)? - angularWhat property of the Drupal 8.1.x Rest API in a JSON request controls the Drupal File status logic (Temporary and Permanent)? - jsonHow to animate a user view in a user view - iosAll Articles