Data Structures Types of Data Structures Algorithms Algorithms Running times
Algorithm is a specified set of instructions for solving a problem
Various Criteria for Goodness of Algorithm Correctness Performance Running Times (minimum) Memory requirement (minimum) Generality (maximum) Clarity (maximum)
Example: Insertion sort Input: 5, 2, 4, 6, 1, 3 Output: 1, 2, 3, 4, 5, 6
Example: Insertion sort Input: A= {5, 2, 4, 6, 1, 3} Output: A={1, 2, 3, 4, 5, 6} i j sorted key n=length[A]=6
Insertion Sort InsertionSort(A) 5 2 4 6 1 3 for j2 to length[A] do keyA[j] ij-1 while i>0 and A[i]>key do A[i+1]A[i] ii-1 A[i+1]key 5 2 4 6 1 3 2 5 4 6 1 3 2 4 5 6 1 3 1 2 4 5 6 3 1 2 3 4 5 6
Insertion Sort –Running Time Cost Times C1 n C2 n-1 C3 n-1 C4 C5 C6 C7 n-1 InsertionSort(A) for j2 to length[A] do keyA[j] ij-1 while i>0 and A[i]>key do A[i+1]A[i] ii-1 A[i+1]key
Insertion Sort –Running Time Cost Times C1 n C2 n-1 C3 n-1 C4 C5 C6 C7 n-1 T(n)=C1n+ C2(n-1)+C3(n-1)+ + C7(n-1)+
Running Time Algorithm depends on: Input size (n) Input itself (e.g. partially sorted) Speed of primitive operations(constants) (will be ignored in future)
Running Time Algorithm Kinds of Analysis: Worst Case: T(n)=max time of any input size n (Upper Bound) Average Case: T(n)=average time of any input size n Best Case: (Lower bound)
Insertion Sort The best case: A is already sorted tj=1 for any j T(n)=C1n+ C2(n-1)+C3(n-1)+ C7(n-1)+ C4(n-1) = (C1 + C2 +C3 + C4+ C7 )n-(C2+ C3+ C4+ C7) = an+b T(n) is linear function of n
Insertion Sort The worst case: A is sorted in reverse order (A[j] is compared with all elements in A[1…j-1]) tj=j for each j T(n)=C1n+ C2(n-1)+C3(n-1)+ C7(n-1)+ = (C1 + C2 +C3 + C7 )n-(C2+ C3+ C7)+ C4 (n(n+1)/2-1) +(C5+ C6 )n(n-1)/2 = A n2+B n+C T(n) is quadratic function of n
Insertion Sort The average case: tj=j/2 for each j T(n) is still quadratic function of n
Insertion Sort-The worst case and Average case Asymptotic Analysis (large n) Notations Example: T(n)=3n2+5n-2= (n2) For large n average case is not much better than the worst (g(n)) ( will be defined formally later)