220 likes | 500 Views
CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?. Dr. Juman Byun The George Washington University. Please drop this course if you have not taken the following prerequisite. Sometimes enthusiasm alone is not enough. CSci 1311: Discrete Structures I (3)
E N D
CSCI 6212 Design and Analysis of AlgorithmsWhich algorithm is better ? • Dr. Juman Byun • The George Washington University • Please drop this course if you have not taken the following prerequisite. Sometimes enthusiasm alone is not enough. • CSci 1311: Discrete Structures I (3) • CSci 1112: Algorithms and Data Structures (3)
Example: Running Time Analysis • Insertion Sort (A) • 1 for j = 2 to A.length • 2 key = A[j] • 3 // Insert A[j] into the sorted sequence A[1..j-1] • 4 i = j - 1 • 5 while i > 0 and A[i] > key • 6 A[i +1] = A[i] • 7 i = i - 1 • 8 A[i + 1] = key
Best Case: already sorted Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i = j - 1 5 while i > 0 and A[i] > key 6 A[i +1] = A[i] 7 i = i - 1 8 A[i + 1] = key
Best Case: already sorted Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i = j - 1 5 while i > 0 and A[i] > key 6 A[i +1] = A[i] 7 i = i - 1 8 A[i + 1] = key
Best Case: already sorted • simply express it
Worst Case: reverse sorted Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i = j - 1 5 while i > 0 and A[i] > key 6 A[i +1] = A[i] 7 i = i - 1 8 A[i + 1] = key
Worst Case: reverse sorted Insertion Sort (A) 1 for j = 2 to A.length 2 key = A[j] 3 // Insert A[j] into the sorted sequence A[1..j-1] 4 i = j - 1 5 while i > 0 and A[i] > key // for entire A[1..j-1] 6 A[i +1] = A[i] //∴tj = (j - 1) + 1 7 i = i - 1 // = j 8 A[i + 1] = key
Worst Case: reverse sorted • To abstract running time T(n) using constants • Notation of "Worst-Case Running Time of Insertion Sort"
Worst Case: reverse sorted • Simply abstract it using constants
To Denote Relative Algorithm Performance • Algorithm 1 input size n with running time f(n) • Asymptotic Notation