# Algorithms | Analysis of Algorithms | Question 8

What is the time complexity of the below function?

`void` `fun(` `int` `n, ` `int` `arr[])` `{` ` ` `int` `i = 0, j = 0;` ` ` `for` `(; i < n; ++i)` ` ` `while` `(j < n && arr[i] < arr[j])` ` ` `j++;` `}` |

**(A)** O(n)**(B)** O(n^2)**(C)** O(nlogn)**(D)** O(n(logn)^2)**Answer:** **(A)****Explanation:** In the first look, the time complexity seems to be O(n^2) due to two loops. But, please note that the variable **j** is not initialized for each value of variable **i**. So, the inner loop runs at most **n** times. Please observe the difference between the function given in question and the below function:

`void` `fun(` `int` `n, ` `int` `arr[])` `{` ` ` `int` `i = 0, j = 0;` ` ` `for` `(; i < n; ++i)` ` ` `{` ` ` `j = 0;` ` ` `while` `(j < n && arr[i] < arr[j])` ` ` `j++;` ` ` `}` `}` |