Download presentation
Presentation is loading. Please wait.
1
Data Structures Types of Data Structures Algorithms
Algorithms Running times
2
Algorithm is a specified set of instructions for solving a problem
3
Various Criteria for Goodness of Algorithm
Correctness Performance Running Times (minimum) Memory requirement (minimum) Generality (maximum) Clarity (maximum)
4
Example: Insertion sort
Input: 5, 2, 4, 6, 1, 3 Output: 1, 2, 3, 4, 5, 6
5
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
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
7
Insertion Sort –Running Time
Cost Times C1 n C n-1 C n-1 C4 C5 C6 C 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
8
Insertion Sort –Running Time
Cost Times C1 n C n-1 C n-1 C4 C5 C6 C n-1 T(n)=C1n+ C2(n-1)+C3(n-1)+ + C7(n-1)+
9
Running Time Algorithm depends on:
Input size (n) Input itself (e.g. partially sorted) Speed of primitive operations(constants) (will be ignored in future)
10
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)
11
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
12
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
13
Insertion Sort The average case:
tj=j/2 for each j T(n) is still quadratic function of n
14
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.