Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms.

Similar presentations


Presentation on theme: "Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms."— Presentation transcript:

1 Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms

2 Some useful terminology Time and Space Complexity: analyze not just the number of steps, but also the space usage. Worst case analysis versus other types (best case, average)

3 Time and Space Complexity Time: number of steps Space: number of memory cells used by the algorithm, besides the input Examples of space usage: Find Min, Search, Selection Sort, Insertion Sort: no extra space (constant, in fact: O(1) ) Merge: extra n space, O(n) Merge Sort: O(n) extra space

4 Best case, Average case Sequential Search: worst case O(n), best case O(1), average case n/2= O(n) Binary Search: best case O(1), worst case and average O(log n) QuickSort: worst case O(n 2), average case O(n log n)

5 Analysis of Iterative Algorithms Examples: Selection Sort Insertion Sort Correctness proof using loop invariant for Selection Sort (pb. 1.2 textbook ) Summation formulas

6 Selection Sort Applet and Pseudocode from Dominique’s page Proof using loop invariant Analysis: –Time to find minimum M(n)=n-1 –Time to do Selection Sort: T(n) = M(n)+M(n-1)+…+ M(2)+M(1) = = 1+2+…+(n-1) = n(n-1)/2 = O(n 2 )

7 Insertion Sort Applet and pseudocode from Dominique’s page Proof of correctness using loop invariant Analysis: –Time to search and shift at step k: S(k)=k –Time to do Insertion Sort: T(n) = S(1)+S(2)+…+S(n-1)=1+2+…+(n-1)= = n(n-1)/2 = O(n 2)

8 Other examples from textbook


Download ppt "Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms."

Similar presentations


Ads by Google