SLIDE 1
CS 171: Introduction to Computer Science II Algorithm Analysis + - - PowerPoint PPT Presentation
CS 171: Introduction to Computer Science II Algorithm Analysis + - - PowerPoint PPT Presentation
CS 171: Introduction to Computer Science II Algorithm Analysis + Simple Sorting Li Xiong Today Algorithm Analysis (cont) Simple sorting algorithms Tilde Notation
SLIDE 2
SLIDE 3
Tilde Notation
- +
SLIDE 4
Big-Oh Notation
Given functions and , we say that is if there are positive constants and such that
- and such that
≤ for ≥ Example: + is
pick = and =
SLIDE 5
Important Functions in Big-Oh Analysis
Constant: Logarithmic: log Linear: N-Log-N: log Quadratic: Cubic: Cubic: Polynomial: Exponential: Factorial:
SLIDE 6
SLIDE 7
Practical method for Big-Oh Analysis
Write down cost function
1.Look for highest-order term (tilde notation) 2.Drop constant factors
Examples
SLIDE 8
Common notations for algorithm analysis
SLIDE 9
Useful Approximations
Harmonic sum 1 + 1/2 + 1/3 + … + 1/N ~ lnN Triangular sum 1 + 2 + 3 + … + N = N(N+1)/2 ~ N2/2 1 + 2 + 3 + … + N = N(N+1)/2 ~ N /2 Geometric sum 1 + 2 + 4 + … + N = 2N -1 ~ 2N when N = 2n Stirling’s approximation lg N! = lg1 + lg2 + lg3 + … + lgN ~ NlgN
SLIDE 10
Common order-of-growth classifications
SLIDE 11
Practical implications of Order-or-growth
SLIDE 12
Example 4
SLIDE 13
Example 4
- +
- +−+−+++ =
- 0.5 ( n2 + n) O(n2)
SLIDE 14
Example 5
SLIDE 15
Example 5: Solution
- This has a logarithmic cost:
- r as the change of base is merely a
matter of a constant factor.
SLIDE 16
Example 6
SLIDE 17
Example 6
What about this:
- ++!+"++
SLIDE 18
Review Question
What is the Order of growth (big-oh) of the following code? for (int i=1; i<=N; ++i){ for (int j=1; j<=N; j*=2){ count++; count++; } }
SLIDE 19
Search in Ordered vs. Unordered Array
SLIDE 20
Search in Ordered vs. Unordered Array
- Binary search has much better running time,
Binary search has much better running time, particularly for large-scale problems
SLIDE 21
Today
Algorithm Analysis (cont) Simple sorting algorithms
SLIDE 22
Sorting problem
SLIDE 23
Sorting Problem
SLIDE 24
Sorting Problem
How do you sort a hand of poker cards?
SLIDE 25
Simple sort
Bubble sort Selection sort Insertion sort
SLIDE 26
Two useful sorting abstractions
SLIDE 27
Bubble Sort
Intuition:
# Find the biggest number. # Find the second biggest number. # Find the third biggest number. # Find the third biggest number. # …
This gives you an ordering of the numbers. Bubble sort achieves this by repeatedly swapping two adjacent numbers.
SLIDE 28
SLIDE 29
After one pass, we find the biggest number.
Bubble Sort
It’s like the biggest ‘bubble’ floats to the top
- f the surface, hence the name ‘bubble sort’.
SLIDE 30
In the second pass, we repeat the same process, but now we only have N-1 numbers to work on. The third pass is the same, with only N-2
Bubble Sort
The third pass is the same, with only N-2 numbers. … Repeat until all players are in order.
SLIDE 31
Analysis of Bubble Sort
Number of comparisons? Number of swaps? Number of swaps?
SLIDE 32
=
Analysis of Bubble Sort
Number of comparisons? Number of swaps?
- −
- =
Number of swaps? best case: worst cast: average:
=
- −
- −
!
SLIDE 33
Selection Sort
1. Keep track of the index of the smallest number in each round. 2. Swap the smallest number towards the beginning of the array. beginning of the array. 3. Repeat the above two steps.
SLIDE 34
Selection Sort
SLIDE 35
Selection Sort
SLIDE 36
Selection Sort Implementation
SLIDE 37
Selection Sort
Online demo
http://www.sorting-algorithms.com/selection-sort
Gypsy dance demo
http://www.youtube.com/watch?v=Ns4TPTC8whw
SLIDE 38
Selection Sort
Number of comparisons? Number of swaps?
SLIDE 39
- Selection Sort
Number of comparisons?
- Number of swaps?