13.6 Simplified Analysis Process
It's not that simple.
Last updated
It's not that simple.
Last updated
Summary of our (Painful) Analysis Process
Construct a table of exact counts of all possible operations (takes lots of effort!)
Convert table into worst case order of growth using 4 simplifications.
We will now propose an alternative method that avoids building a table altogether!
Our simplified analysis process will consist of:
Choosing our cost model, which is the representative operation we want to count.
Figuring out the order of growth for the count of our representative operation by either:
Making an exact count and discarding unnecessary pieces or...
Using intuition/inspection to determine orders of growth. This is something that comes with practice.
Find the order of growth of the worst case runtime of dup1
.
We will choose our cost model to be the number of == operations.
Looking at the structure of the loops, the inner loop first gets run j=N-1 times. At the second iteration, i=1, so the inner loop runs an additional j=N-2 times. At the third iteration, i=2, so the inner loop runs an additional j=N-3 times. The total number of times the loop is run is thus:
This cost can be simplified to (how?). We can use simplification to throw away all lower order terms and constants to get the worst case order of growth .
We can see that the number of equals can be given by the area of a right triangle, which has a side length of .
Therefore, the order of growth of area is .
This is definitely not something that is immediately obvious. It takes time and practice to see these patterns!