order of complexity. consider four algorithms 1.the naïve way of adding the numbers up to n 2.the...

12
Order of complexity

Upload: natalie-cruz

Post on 26-Mar-2015

214 views

Category:

Documents


0 download

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