Skip to main content
added 171 characters in body; edited title
Source Link
Yuval Filmus
  • 279.1k
  • 27
  • 316
  • 512

Time Complexity when there iscomplexity of a function with while loop

enter image description here What is the time complexity of the following procedure?

for $x \in \{1,\ldots,n\}$:
$\quad$ $i \gets \lfloor n/2 \rfloor$
$\quad$ while $i \neq x$:
$\quad\quad$ if $i > x$ then $i \gets i -1$, otherwise $i \gets i + 1$.

According to me, the time complexity will be O(n^2),is $O(n^2)$. theThe inner loop while startloop starts at N/2$n/2$ and movemoves towards the value of x$x$, Worstworst case it runs N/2$n/2$ times, and best case it runs 0$0$ times, but I am still wanna know what could be the exact time complexity, can anyone help me to understand it, please? Thanks.

What is the exact time complexity of this procedure?

Time Complexity when there is while loop

enter image description here

According to me, the time complexity will be O(n^2), the inner loop while start at N/2 and move towards the value of x, Worst case it runs N/2 times and best case it runs 0 times, but I am still wanna know what could be the exact time complexity, can anyone help me to understand it, please? Thanks

Time complexity of a function with while loop

What is the time complexity of the following procedure?

for $x \in \{1,\ldots,n\}$:
$\quad$ $i \gets \lfloor n/2 \rfloor$
$\quad$ while $i \neq x$:
$\quad\quad$ if $i > x$ then $i \gets i -1$, otherwise $i \gets i + 1$.

According to me, the time complexity is $O(n^2)$. The inner while loop starts at $n/2$ and moves towards the value of $x$, worst case it runs $n/2$ times, and best case it runs $0$ times.

What is the exact time complexity of this procedure?

Source Link
kiv
  • 339
  • 2
  • 8

Time Complexity when there is while loop

enter image description here

According to me, the time complexity will be O(n^2), the inner loop while start at N/2 and move towards the value of x, Worst case it runs N/2 times and best case it runs 0 times, but I am still wanna know what could be the exact time complexity, can anyone help me to understand it, please? Thanks