Orders of common functions
When we compare the Big-O of two algorithms, we are comparing at the end how the running time and space requirements grow depending on the input data. We need to know how the algorithm will behave with any amount of data. Let's see the orders of common functions in ascending order.
O(1)
When the running time is constant, always with the same value, we have O(1). So the algorithm space/running time is not dependent on the input data. One example is the time needed to access an item in an array with the index. It uses just one instruction (at a high level) to do it. The pop function on a stack is another example of O(1) operations. The space complexity of the insertion sort also uses just one memory register, so it is O(1).
Here is an example:
public func firstElement(array:[Int]) -> Int? { return array.first }
Here we have a very simplified function that receives an array of integers and returns the first one (if it exists). This is...