90 likes | 123 Views
Explore the time and space complexity of iterative sorting algorithms, including worst case versus best case scenarios. Learn about examples like Selection Sort and Insertion Sort, with proof of correctness and analysis using loop invariants. Understand the space usage and time taken for each algorithm, such as QuickSort, Sequential Search, and Binary Search. Delve into summation formulas and computations to analyze these algorithms thoroughly.
E N D
Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms
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)
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
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)
Analysis of Iterative Algorithms • Examples: • Selection Sort • Insertion Sort • Correctness proof using loop invariant for Selection Sort (pb. 1.2 textbook) • Summation formulas
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(n2)
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)