39
CS171: Introduction to Computer Science II Algorithm Analysis + Simple Sorting Li Xiong

CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:

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:

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:

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:

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:

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:
Page 7: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:
Page 29: CS171:Introduction to Computer Science II Algorithm ...€¦ · Simple sort •Bubble sort •Selection sort •Insertion sort. Two useful sorting abstractions. BubbleSort • Intuition:

• 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:

• 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:

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:

= 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:

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:

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:

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:

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:

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:

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:

O(N2)

Selection Sort

• Number of comparisons?

O(N2)

• Number of swaps?

O(N)