CS2
Week 5: Algorithm analysis, searching, sorting
3
Time analysis of algorithms
(AKA run-time analysis, time complexity, program complexity, order of magnitude)
nSuppose you want to judge two algorithms, A and B, to find which was better in terms of running time. You implement both A and B, run them and discover that A took 2 min and B took 1 min 45 sec.
nDoes this prove that B is better than A?
nWhat if you standardized a computer, OS, test data, etc.?
nInstead of measuring time computer scientist would calculate a number of steps necessary to complete an algorithm. A step is a simple operation like an assignment, comparison, a simple arithmetic operation, etc.
nEx. Problem: Calculate the sum of all the integers from 1 to n.
nAlg.#1: int sum=0; for (int count=1; count<=n; count++) sum += count;
nAlg. #2 int sum=((n+1)*n)/2;
n