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.

Post on 26-Mar-2015

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

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 of n sorted items4. An insertion sort of n items

Naïve summing of integers

Smart summing of integers

Binary search

Insertion Sort

Outer loop, increases the sorted section

Inner loop, steps backwards to find The ‘right’ place to swap the unsorted elements

Insertion Sort

Quicksort

Pivot Value

Smaller than the Pivot

Larger than the Pivot

http://cs.slu.edu/~goldwasser/demos/Quicksort/

Quicksort

Pivot Value

Smaller than the Pivot

Larger than the Pivot

http://cs.slu.edu/~goldwasser/demos/Quicksort/

Quicksort

Pivot Value

Smaller than the Pivot

Larger than the Pivot

http://cs.slu.edu/~goldwasser/demos/Quicksort/

Quick Sort

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

top related