Just try some examples to see why the worst-case complexity is $O(N)$.
For the first code, you have two nested for loops.
- First loop:
for i = N downto 1 and for each iteration i the loop halves, i.e., i=N then i=N/2, etc.
- Second loop:
for j = 0 to i and you increase j by 1.
So, we have:
- For
i=N, you would need to run the second loop from j=0 to N. This
costs, roughly, N iterations. Now, i=N/2.
- For
i=N/2, you would need to run the second loop from j=0 to N/2. This
costs, roughly, N/2 iterations. Now, i=N/4.
- For
i=N/4, you would need to run the second loop from j=0 to N/4. This
costs, roughly, N/4 iterations. Now, i=N/8.
- For
i=N/8, you would need to run the second loop from j=0 to N/8. This
costs, roughly, N/8 iterations.
You would have to do these steps, 1., 2., 3., 4., etc., until $i=1$. It is not hard to see that the number of steps is equal to $\lg N$, since you start with $N$ and you half it each time until you get $1$.
Hence, the number of steps is, roughly, equal to $\lg N$.
Now, how much does each step costs? Well we can easily see that step $i$ costs, roughly, $N/2^{i-1}$ iterations (in the second loop for j). Therefore, the worst-case complexity of the first code is, roughy:
Cost of each step x how many steps which can be calculated as:
\begin{align}
\underbrace{N+N/2+N/4+N/8+\ldots+1}_{\lg N\text{ times }} &= N\sum_{i=0}^{\lg N}\dfrac{1}{2^i}\\
&\overset{(a)}{=}N\dfrac{1-(1/2)^{\lg N + 1}}{1-1/2}\\
&=2N(1-(1/2)(1/2)^{\lg N})\\
&=N(2-1/N)\\
&=2N-1=O(N),
\end{align}
where $(a)$ is due to the geometric sum.
Finally, the complexity is $O(N)$ which is linear.
You can prove the same thing for the second code.