The outer foreach
is executed n = |c1
| times (where |x| is the size of c1
), while the inner foreach
is executed m = |c2
| times. That's O(n * m) times in total.
how would I represent simple algorithms with the following complexities?
This is the same as O(n^2). Something that takes O(n^2) time would be drinking a toast with every other person at a party, assuming that there's always exactly two people in a toast, and only one person does the toasting at a time.
Same as above; the O(n^2) term dominates. Another example of an O(n^2) effort is planting trees in a square garden of length n
, assuming it takes constant time to plant each tree, and that once you plant a tree other trees are excluded from its vicinity.
An example of this would be finding a word in a dictionary by repeatedly picking the midpoint of the region of pages you need to search next. (In other words, a binary search.)
Use the above algorithm, but now you have to find every word in the dictionary.