Code complexity

What is the complexity of a program with only one loop, is log n? can someone give me some ideas on evaluating code complexity?

+3
source share
6 answers

Well, it really depends on what happens in this cycle.

This cycle is linear time, i.e. O (n):

int sum = 0;
foreach( int i in SomeCollection )
{
    sum += i;
}

However, consider a loop that searches for a substring during each iteration. Now you need to consider the complexity of the string search algorithm. Your question cannot be answered because it is worth it. You will need to provide a sample code if you want a meaningful answer.

+6
source

Software complexity

There is a field of research that is trying to pinpoint this.

.

, , p = 2, , , .

, O (1), , , , - .

+3

Big-O, : " "?

, , , , .

:

For (i = 0 ; i < n ; i++ ) {
   //Do stuff
}

on

For (i = n ; i > 0 ; i= i/2 ) {
   //Do stuff
}

logn... .

void Mess (int n) {
    for (i = 0 ; i < n ; i++) {
         //Do stuff
         Mess(n-1);
    } 
}

, , , ... n n-1. . n == 1, 1 . n == 2, .
, enter image description here, , : In , , n!

, .

+2

, , , , ... , , . , n times O(n^2) , strlen, memcpy ..

, , ( ) , (, Perl), O(2^n)!! .

+1

Big-O, , n times , , n - .

. , .. O (1), O (1 * n) = O (n).

, , m, O (n * m) ..

0

Source: https://habr.com/ru/post/1788554/


All Articles