1
$\begingroup$

enter image description here

In this loop $j$ is dependent on $i$, and $j$ is executing like $n/2$, $n/2-1$, $n/2-2$, ... So does this sum up to $O(n\log n)$?

$\endgroup$
2
$\begingroup$

We just have one comparison when $n$ is odd. So, the total complexity is (if we suppose $n$ is even, w.l.o.g.):

$$T(n) = \frac{n}{2} + (n +‌ (n-2) + (n-4) + ... + 2) $$ $$= \frac{n}{2} + 2 \sum_{i=1}^{\frac{n}{2}}i = \frac{n}{2} + 2\frac{\frac{n}{2}(\frac{n}{2}+1)}{2} = \Theta(n^2)$$

$\endgroup$
  • 1
    $\begingroup$ j is dependent on i, and j is executing like n/2 , n/2−1, n/2−2 ... isnt it so? $\endgroup$ – Turing101 Nov 4 '19 at 15:13
  • 1
    $\begingroup$ @HIRAKMONDAL How did you get $\frac{n}{2}$? $\endgroup$ – OmG Nov 4 '19 at 15:15
  • $\begingroup$ for i==0 how many times j loop executes? $\endgroup$ – Turing101 Nov 4 '19 at 15:18
  • 1
    $\begingroup$ @HIRAKMONDAL $n$ times. $\endgroup$ – OmG Nov 4 '19 at 15:29
  • 1
    $\begingroup$ @HIRAKMONDAL Yes. indeed you tried to count the number of printf. $\endgroup$ – OmG Nov 4 '19 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.