Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ? Dr. Juman Byun The George Washington University Please drop this course if you.

Similar presentations


Presentation on theme: "CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ? Dr. Juman Byun The George Washington University Please drop this course if you."— Presentation transcript:

1 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) CSci 1112: Algorithms and Data Structures (3)

2 Running Time Calculation

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] 4i = j - 1 5while i > 0 and A[i] > key 6A[i +1] = A[i] 7i = i - 1 8A[i + 1] = key LineCostTimes 1c1n 2c2n-1 3c3n-1 4c4n-1 5c5 6c6 7c7 8c8n-1

4 LineCostTimes 1c1n 2c2n-1 3c3n-1 4c4n-1 5c5 6c6 7c7 8c8n-1

5 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] 4i = j - 1 5while i > 0 and A[i] > key 6A[i +1] = A[i] 7i = i - 1 8A[i + 1] = key

6 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] 4i = j - 1 5while i > 0 and A[i] > key 6A[i +1] = A[i] 7i = i - 1 8A[i + 1] = key

7 Best Case: already sorted

8 simply express it

9 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] 4i = j - 1 5while i > 0 and A[i] > key 6A[i +1] = A[i] 7i = i - 1 8A[i + 1] = key

10 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] 4i = j - 1 5while i > 0 and A[i] > key// for entire A[1..j-1] 6A[i +1] = A[i]// ∴ t j = (j - 1) + 1 7i = i - 1// = j 8A[i + 1] = key

11 Worst Case: reverse sorted

12

13

14

15

16

17

18

19 To abstract running time T(n) using constants Notation of "Worst-Case Running Time of Insertion Sort"

20 Worst Case: reverse sorted Simply abstract it using constants

21 To Denote Relative Algorithm Performance Algorithm of input size n with running time f(n) Asymptotic Notation

22 O and o Notation "Big O" vs "little o"

23 Θ "Theta"

24 Ω and ω Notation "Omega" vs "little omega"

25 Why is O popular ?


Download ppt "CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ? Dr. Juman Byun The George Washington University Please drop this course if you."

Similar presentations


Ads by Google