Top Banner
Order of complexity
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.

Mar 26, 2015

Download

Documents

Natalie Cruz
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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