0

I designed an algorithm like this but I do not know how to perform its analysis to determine how it runs $($$O(n)$, $O(n^2)$, $O(log(n)$, $etc$$)$

m = some constant
for i = 1 to n
{
    for j = 1 to m step 2
    {
        // do some stuff here
    }
    m = m - 2
}

Can you please provide a clue to solve my issue? This is a homework and I only have to deliver the algorithm, but I want to go farther and determine its behavior. I have analyzed other (maybe simpler) algorithms but this is kind of different of what I have seen.

Respectfully,

Jorge Maldonado

Raphael
  • 72,336
  • 29
  • 179
  • 389
JORGE
  • 259
  • 3
  • 8
  • 1
    http://cs.stackexchange.com/questions/23593/is-there-a-system-behind-the-magic-of-algorithm-analysis – adrianN Sep 23 '16 at 14:22
  • I have already seen the "Is there a system behind the magic of algorithm analysis?" but I cannot solve my question yet. This is my first class on Analysis of Algorithms, I am a complete beginner. My main issue is in line $m = m - 2$. – JORGE Sep 23 '16 at 15:27
  • @JORGE The program is essentially equivalent to m=some constant/2 ... for j=1 to m { ... } m=m-1. – David Richerby Sep 23 '16 at 15:37
  • @JORGE If you can't solve your problem with the reference question, you should have more specific questions than this here. See also many questions tagged [tag:algorithm-analysis+loops]. – Raphael Sep 23 '16 at 15:46
  • Hint: Use that the value of m in iteration $i$ is $m_i = M - 2(i-1)$ with $M$ the initial value of m. – Raphael Sep 23 '16 at 15:47
  • The key idea here is that the inner loop will iterate one less time for each value of $i$ in the outer loop. That means that after a certain number of iterates of the outer loop, the inner loop will do nothing (except for the test) and so we'll have the asymptotic run time for large enough $n$ will be $O(n)$ plus the constant time for the initial iterates of the inner loop (which we can ignore). – Rick Decker Sep 23 '16 at 15:50
  • @RickDecker Since $m$ is a constant, how exactly the inner loop depends on $m$ does not even matter, as long as it depends only on $m$. (I'd preferred JORGE had reached this insight on their own, but well.) – Raphael Sep 23 '16 at 16:55
  • @Raphael. Which is why I put in the parenthesized clause at the very end. – Rick Decker Sep 23 '16 at 17:32

0 Answers0