Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 2 Introduction to Asymptotic Analysis Read Ch. 2 and 3 of Text 1.

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 2 Introduction to Asymptotic Analysis Read Ch. 2 and 3 of Text 1."— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 2 Introduction to Asymptotic Analysis Read Ch. 2 and 3 of Text 1

2 Recall Insertion Sort Idea: Sort items one at a time into previously sorted group. Invariant: After the ith step of the algorithm, A[1..i] is sorted. 2

3 Pseudocode for Insertion Sort
Insertion-Sort(A) for j = 2 to A.length key = A[j] i = j - 1 while i > 0 and A[i] > key A[i+1] = A[i] i = i - 1 A[i+1] = key Cost c1 c2 c3 c4 c5 c6 c7 times n n-1 ? 5

4 Analyzing the while loop
Let tj = number of times the while loop test is executed for a given value of j. tj depends on the initial order of the array. Total number of times the while loop test is executed: The body of the while loop is executed one less time than the while loop test for each value of j:

5 Pseudocode for Insertion Sort
Insertion-Sort(A) for j = 2 to A.length key = A[j] i = j - 1 while i > 0 and A[i] > key A[i+1] = A[i] i = i - 1 A[i+1] = key Cost c1 c2 c3 c4 c5 c6 c7 times n n-1 ____ 5

6 What is tj? Best Case Best Case: All items in sorted order
Nothing needs to be shifted While loop test executed once for each j Body executed 0 times for each j test execution: body execution: 6

7 Best Case--Adding it up
Insertion-Sort(A) for j = 2 to A.length key = A[j] i = j - 1 while i > 0 and A[i] > key A[i+1] = A[i] i = i - 1 A[i+1] = key Cost c1 c2 c3 c4 c5 c6 c7 times n n-1 ____ T(n) = c1n + c2(n-1) + c3(n-1) + c4(n-1) + c7(n-1) = (c1 + c2 + c3 + c4 + c7) n - (c2 + c3 + c4 + c7) = an - b Best Case is Linear!

8 What is tj? Worst Case Worst Case: All items in reverse sorted order
Must look through all previously sorted items to place each item. While loop test executed j times for each j Body executed j-1 times for each j test execution: body execution:

9 Worst Case--Adding it up
Insertion-Sort(A) for j = 2 to A.length key = A[j] i = j - 1 while i > 0 and A[i] > key A[i+1] = A[i] i = i - 1 A[i+1] = key Cost c1 c2 c3 c4 c5 c6 c7 times n n-1 ______ T(n) = (c4/2 + c5/2 + c6/2)n2 + (c1 + c2 + c3 + c4/2 - c5/2 - c6/2 + c7) n - (c2 + c3 + c4 + c7) = an2 + bn + c Worst Case is quadratic! 13

10 Recall Merge Sort Divide in 1/2 repeatedly merge in sorted order
T(n) = c1n lg(n) + c1n How does this compare to Insertion sort? 14

11 Asymptotic analysis Measuring Growth Rates:
When comparing the running time of two algorithms, a good measure is how fast the running time increases as the input size increases. When comparing growth rates, we can ignore all but the highest order term. When comparing growth rates, we can ignore constant coefficients. We would like a coarse way to compare functions that ignores constants and focuses on their relative growth as the input size gets large. 15

12 Examples of Growth Rate
n=1 n=1000 n=1,000,000 p(n) = 100n q(n) = 3n2 + 2n + 1 r(n) = 0.1n2 Lower order terms become insignificant as n increases. r and q are quadratic. They grow much faster than p. Asymptotic notation gives us a way to express this relationship: Next time we will learn the mathematical definitions of these. 16


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 2 Introduction to Asymptotic Analysis Read Ch. 2 and 3 of Text 1."

Similar presentations


Ads by Google