saw792
Is known to say things. That is all.
- Reaction score
- 280
Big O notation (eg: O(1), O(n), O(n^2), O(log n) etc) is a way you can represent the complexity of an algorithm or operation, in other words how well it scales.
For example, right now every time getPointGridSquare is called it will run the code inside the loop once for every square until it finds the correct square. That's called a linear search, i.e. you search through every element until one satisfies the condition in counting order. This means that with a grid of n squares it will take on average n/2 time to find the correct square (sometimes over, sometimes under). Thus the amount of time it takes for your algorithm to find the correct square increases linearly with an increase in the number of squares on the grid. So if you double the number of squares you double the (average) amount of time it takes for your code to run. Thus O(n) is used to describe your algorithm as it is a linear relationship between time to run and number of squares.
My approach is to find a mathematical way of calculating which square the coordinates will be in. This means that regardless of the number of grid squares in the grid it will take the same amount of time to find the correct square. Thus an algorithm of that sort would be O(1), as the time taken is constant regardless of the number of grid squares n.
EDIT: getPointGridSquare:
Oh and your getSquare method should have some error checking to make sure the coordinates actually exist in the grid. That way getPointGridSquare doesn't need to duplicate the checks.
For example, right now every time getPointGridSquare is called it will run the code inside the loop once for every square until it finds the correct square. That's called a linear search, i.e. you search through every element until one satisfies the condition in counting order. This means that with a grid of n squares it will take on average n/2 time to find the correct square (sometimes over, sometimes under). Thus the amount of time it takes for your algorithm to find the correct square increases linearly with an increase in the number of squares on the grid. So if you double the number of squares you double the (average) amount of time it takes for your code to run. Thus O(n) is used to describe your algorithm as it is a linear relationship between time to run and number of squares.
My approach is to find a mathematical way of calculating which square the coordinates will be in. This means that regardless of the number of grid squares in the grid it will take the same amount of time to find the correct square. Thus an algorithm of that sort would be O(1), as the time taken is constant regardless of the number of grid squares n.
EDIT: getPointGridSquare:
JASS:
Oh and your getSquare method should have some error checking to make sure the coordinates actually exist in the grid. That way getPointGridSquare doesn't need to duplicate the checks.