Hello it has been some time sins i have used Big O notation, so i'm a bit rusty.
I know that having 1 loop, that loops n times is O(n), and having 1 loop that loops n time within another loop that loops n times is O(n^2).
But in the following code snippet do i have a loop that loops n times, and within that a loop that loops n-i times.
What will the Worst and Best O notation be for this code?
The worst is where it runs without finding a collision, and the best will be when there are a collission between the 2 first rectangles.
class TestClass
{
bool Run(List<Rectangle> R)
{
var b = false;
var i = 0;
var n = R.Count;
var j = 0;
while ((i < n-1 ) && !b )
{
j = i + 1;
while (j<n && !b)
{
if ((R[i].x >= R[j].x - R[i].w) && (R[i].x <= R[j].x + R[j].w))
if ((R[i].y <= R[j].y + R[i].h) && (R[i].y >= R[j].y - R[j].h))
b = true;
j++;
}
i++;
}
return b;
}
}
public class Rectangle
{
public int x;
public int y;
public int w;
public int h;
}