2. Big O notation describes algorithms
• Algorithms take input and provide some kind of output
• Could be returning TRUE or FALSE
• Could return a number or an array of numbers
• Could be something else!
• Tells how the time and CPU space required for computations
change as the size of the input increases
Answers the question: as I increase the input, how much more CPU
time and space will be required for my algorithm to run?
3. Examples to illustrate: O(1)
Suppose your algorithm was:
“Print the jth element of the array of size”
You can directly access the jth element of the array, so no matter
how big the array gets, the time it takes to access the 5th element
won’t change.
4. Examples to illustrate: O(n)
Suppose your algorithm was:
“Print each element of the array of size n”
As you increase the size of the array, the time for this algorithm will
increase proportionally. An array of 50 will take twice the time as
an array of 25 and half the time of an array of 100.
5. Examples to illustrate: O(n2)
Suppose your algorithm was:
“Compare each element of an array of size n to each other element
of the array.”
If you add one element to array, you need to go through the entire
rest of the array one additional time. As the input grows, the time
required to run the algorithm will grow quite quickly.
6. Why does it matter?
This is x2
This is x
If your app might be taking in large inputs,
you don’t want to be slowed down by a
needlessly inefficient algorithm.Size of the input
Computationaltime