cs171:introduction to computer science ii algorithm ...€¦ · simple sort •bubble sort...
TRANSCRIPT
![Page 1: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/1.jpg)
CS 171: Introduction to Computer Science II
Algorithm Analysis + Simple Sorting
Li Xiong
![Page 2: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/2.jpg)
Today
• Algorithm Analysis (cont)
• Simple sorting algorithms
![Page 3: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/3.jpg)
Tilde Notation
• Tilde notation: ignore insignificant terms
•Definition: we write f(n) ~ g(n) if f(n)/g(n)
approaches 1 as n grows
• 2n + 10 ~ 2n
• 3n3 + 20n2 + 5 ~ 3n3
![Page 4: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/4.jpg)
Big-Oh Notation
• Given functions f(n) and
g(n), we say that f(n) is
O(g(n)) if there are
positive constants
c and n0
such that 100
1,000
10,000
3n
2n+10
n
c and n0
such that
f(n) ≤ cg(n) for n ≥ n0
• Example: 2n + 10 is O(n)
– pick c = 3 and n0 = 10
1
10
1 10 100 1,000
n
![Page 5: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/5.jpg)
Important Functions in Big-Oh Analysis
–Constant: 1
–Logarithmic: log n
–Linear: n
–N-Log-N: n log n
–Quadratic: n2
–Cubic: n3–Cubic: n3
–Polynomial: nd
–Exponential: 2n
–Factorial: n!
![Page 6: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/6.jpg)
![Page 7: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/7.jpg)
Practical method for Big-Oh Analysis
• Write down cost function f(n)
1. Look for highest-order term (tilde notation)
2. Drop constant factors
• Examples
–3n3 + 20n2 + 5
–n log n + 10
![Page 8: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/8.jpg)
Common notations for algorithm analysis
![Page 9: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/9.jpg)
Useful Approximations
• Harmonic sum
1 + 1/2 + 1/3 + … + 1/N ~ lnN
• Triangular sum
1 + 2 + 3 + … + N = N(N+1)/2 ~ N2/21 + 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
![Page 10: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/10.jpg)
Common order-of-growth classifications
![Page 11: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/11.jpg)
Practical implications of Order-or-growth
![Page 12: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/12.jpg)
Example 4
for (int i = 0; i < n; i ++) {
for (int j = i; j < n; j ++) {
sum += i*j;
}
}}
![Page 13: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/13.jpg)
Example 4
for (int i = 0; i < n; i ++) {
for (int j = i; j < n; j ++) {
sum += i*j;
}
}}
n(n+1)
2n+(n−1)+(n−2)+...+1+0 = is O(n)
0.5 ( n2 + n) � O(n2)
![Page 14: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/14.jpg)
Example 5
double product = 1.0;
for (int i = 1; i <= n; i *= 2) {
product *= i;
}
![Page 15: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/15.jpg)
Example 5: Solution
double product = 1.0;
for (int i = 1; i <= n; i *= 2) {
product *= i;
}
• This has a logarithmic cost:
O(log2 n)
or O(logn) as the change of base is merely amatter of a constant factor.
![Page 16: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/16.jpg)
Example 6
double product = 1.0;
for (int i = 1; i <= n; i *= 2) {
for (int j = 1; j <= i; j ++) {
product *= j;
}
}
![Page 17: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/17.jpg)
Example 6
• What about this:
double product = 1.0;
for (int i = 1; i <= n; i *= 2) {
for (int j = 1; j <= i; j ++) {
product *= j;
}
}
is O(n)1+2+4+8+...+ n
![Page 18: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/18.jpg)
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++;
}
}
![Page 19: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/19.jpg)
Search in Ordered vs. Unordered Array
• What’s the big O function for linear search?
• Binary search?
![Page 20: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/20.jpg)
Search in Ordered vs. Unordered Array
• What’s the big O function for linear search?
O(N)
• Binary search? O(lgN)
• Binary search has much better running time, • Binary search has much better running time,
particularly for large-scale problems
![Page 21: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/21.jpg)
Today
• Algorithm Analysis (cont)
• Simple sorting algorithms
![Page 22: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/22.jpg)
Sorting problem
![Page 23: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/23.jpg)
Sorting Problem
![Page 24: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/24.jpg)
Sorting Problem
• How do you sort a hand of poker cards?
![Page 25: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/25.jpg)
Simple sort
• Bubble sort
• Selection sort
• Insertion sort
![Page 26: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/26.jpg)
Two useful sorting abstractions
![Page 27: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/27.jpg)
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 repeatedlyswapping two adjacent numbers.
![Page 28: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/28.jpg)
![Page 29: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/29.jpg)
• After one pass, we find the biggest number.
Bubble Sort
• It’s like the biggest ‘bubble’ floats to the top
of the surface, hence the name ‘bubble sort’.
![Page 30: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/30.jpg)
• In the second pass, we repeat the same
process, but now we only have N-1 numbersto work on.
• The third pass is the same, with only N-2
Bubble Sort
• The third pass is the same, with only N-2numbers.
• …
• Repeat until all players are in order.
![Page 31: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/31.jpg)
Analysis of Bubble Sort
• Number of comparisons?
• Number of swaps?• Number of swaps?
![Page 32: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/32.jpg)
= O(N )
Analysis of Bubble Sort
• Number of comparisons?
• Number of swaps?
2N(N −1)
2
= O(N )
• Number of swaps?
best case:
worst cast:
average:
= O(N2)
2
N(N −1)
2
N(N −1)
4
O(1)
![Page 33: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/33.jpg)
Selection Sort
1. Keep track of the index of the smallestnumber in each round.
2. Swap the smallest number towards thebeginning of the array.beginning of the array.
3. Repeat the above two steps.
![Page 34: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/34.jpg)
Selection Sort
![Page 35: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/35.jpg)
Selection Sort
![Page 36: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/36.jpg)
Selection Sort Implementation
![Page 37: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/37.jpg)
Selection Sort
• Online demo
–http://www.sorting-algorithms.com/selection-sort
• Gypsy dance demo
–http://www.youtube.com/watch?v=Ns4TPTC8whw
![Page 38: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/38.jpg)
Selection Sort
• Number of comparisons?
• Number of swaps?
![Page 39: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:](https://reader035.vdocuments.net/reader035/viewer/2022071021/5fd5637eb09506473358b7d9/html5/thumbnails/39.jpg)
O(N2)
Selection Sort
• Number of comparisons?
O(N2)
• Number of swaps?
O(N)