Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2018 08:17
Show Gist options
  • Save anonymous/de2424f550fa1bde0f263b0d2b455890 to your computer and use it in GitHub Desktop.
Save anonymous/de2424f550fa1bde0f263b0d2b455890 to your computer and use it in GitHub Desktop.
for(i = 0; i < N; i++)
{
    C[i] = a[i] + b[i];
}

$$t(N) = 1 + N + N + 2N + 8N$$ $$t(N) = 12N + 1$$

$$O(N)$$

for(i = 0; i < N; i++)
{
    for(j = 0; j < N; j++)
    {
        c[i][j] = a[i][j] + b[i][j];
    }    
}

$$t(N) = 11$$ $$1 + N + 2N = 1 + 3N$$ $$1 + 3N + 11N$$ $$11N^2 + N+N^2 + (2N)^2 + 3N + 1 = 14N^2 + 4N + 1$$ $$O(N^2)$$

for(i = 0; i < N; i++)
{
    for(j = i; j < N; j++)
    {
     //   3 |   2  |      2  |
        swap(a[i][j], a[j][i]);
    }
}    

$$t(N) = 3 + 2*3 + 3 + 3 = 15$$ $$S_N = ((a_1 + a_n) * N)/2$$ $$S_N = \frac{((1 +N) * N)}{2} = \frac{(N^2 + N)}{2}$$ $$\frac{N^2 + N}{2} * 15$$ $$(N^2 + N) * 8 = 8N^2 + 8N$$ $$N +(3N)^2 +1 +3N$$ $$T(N) = 8N^2 + 8N + N +9N^2 + 1 + 3N$$ $$T(N)= 17N^2 +12N +1$$ $$O(N^2)$$

c = N;
while(c > 1)
{
    c  = c / 2;
}

$$c > 1$$ $$T(N) = log_2(N) + 2*log_2(N) = 3log_2(N)$$ $$O(log_2(N))$$

c = 1;
while(c <= N)
{
    c  = c * 2;
}

$$c <= N$$ $$T(N) = log_2(N) + 2*log_2(N) = 3log_2(N)$$ $$O(log_2(N))$$

a = 1;
x = 0;
// log_2(N)*N^2 + log_2(N) * (N*log_2(N)) = 
// log_2(N)*N^2
// log_2(N)
while(a <= N)
{
  // n^2
    for(i = 0; i < N; i++)
    {
        for(j = 0; j < N; j++)
        {
            x = x + i + j;
        }
    }
    
    b = N;
    
    // N*log_2(N)
    while(b > 1)
    {
        for(k = 0; k < N; k++)
        {
            y = y + k;
        }
        b = b/2;
    }
    a = a * 2;
}

log_2(N)N^2 + log_2(N) * (Nlog_2(N)) $$log_2(N)N^2 + log_2(N) * (Nlog_2(N))$$ $$log_2(N)*N^2$$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment