order of complexity. consider four algorithms 1.the naïve way of adding the numbers up to n 2.the...
TRANSCRIPT
![Page 1: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/1.jpg)
Order of complexity
![Page 2: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/2.jpg)
Consider four algorithms
1. The ‘naïve’ way of adding the numbers up to n
2. The ‘smart’ way of adding the numbers up to n
3. A binary search of n sorted items4. An insertion sort of n items
![Page 3: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/3.jpg)
Naïve summing of integers
![Page 4: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/4.jpg)
Smart summing of integers
![Page 5: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/5.jpg)
Binary search
![Page 6: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/6.jpg)
Insertion Sort
Outer loop, increases the sorted section
Inner loop, steps backwards to find The ‘right’ place to swap the unsorted elements
![Page 7: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/7.jpg)
Insertion Sort
![Page 8: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/8.jpg)
Quicksort
Pivot Value
Smaller than the Pivot
Larger than the Pivot
http://cs.slu.edu/~goldwasser/demos/Quicksort/
![Page 9: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/9.jpg)
Quicksort
Pivot Value
Smaller than the Pivot
Larger than the Pivot
http://cs.slu.edu/~goldwasser/demos/Quicksort/
![Page 10: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/10.jpg)
Quicksort
Pivot Value
Smaller than the Pivot
Larger than the Pivot
http://cs.slu.edu/~goldwasser/demos/Quicksort/
![Page 11: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/11.jpg)
Quick Sort
![Page 12: Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search](https://reader036.vdocuments.mx/reader036/viewer/2022081518/551444005503466d1a8b5952/html5/thumbnails/12.jpg)
Big O notation
• O(1) – size doesn’t matter, constant– Smart summing
• O(log2n) – Logarithmic - Binary search
• O(n) – linear – increases with size– Naïve summing
• O(n2) – Polynomial – increases with square of the size– Insertion sort