CS 171: Introduction to Computer Science II Algorithm Analysis + - - PowerPoint PPT Presentation

cs 171 introduction to computer science ii algorithm
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

CS 171: Introduction to Computer Science II Algorithm Analysis + Simple Sorting

Li Xiong

slide-2
SLIDE 2

Today

Algorithm Analysis (cont) Simple sorting algorithms

slide-3
SLIDE 3

Tilde Notation

  • +
slide-4
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
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 6
slide-7
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
SLIDE 8

Common notations for algorithm analysis

slide-9
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
SLIDE 10

Common order-of-growth classifications

slide-11
SLIDE 11

Practical implications of Order-or-growth

slide-12
SLIDE 12

Example 4

slide-13
SLIDE 13

Example 4

  • +
  • +−+−+++ =
  • 0.5 ( n2 + n) O(n2)
slide-14
SLIDE 14

Example 5

slide-15
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
SLIDE 16

Example 6

slide-17
SLIDE 17

Example 6

What about this:

  • ++!+"++
slide-18
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
SLIDE 19

Search in Ordered vs. Unordered Array

slide-20
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
SLIDE 21

Today

Algorithm Analysis (cont) Simple sorting algorithms

slide-22
SLIDE 22

Sorting problem

slide-23
SLIDE 23

Sorting Problem

slide-24
SLIDE 24

Sorting Problem

How do you sort a hand of poker cards?

slide-25
SLIDE 25

Simple sort

Bubble sort Selection sort Insertion sort

slide-26
SLIDE 26

Two useful sorting abstractions

slide-27
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 28
slide-29
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
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
SLIDE 31

Analysis of Bubble Sort

Number of comparisons? Number of swaps? Number of swaps?

slide-32
SLIDE 32

=

Analysis of Bubble Sort

Number of comparisons? Number of swaps?

  • =

Number of swaps? best case: worst cast: average:

=

!

slide-33
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
SLIDE 34

Selection Sort

slide-35
SLIDE 35

Selection Sort

slide-36
SLIDE 36

Selection Sort Implementation

slide-37
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
SLIDE 38

Selection Sort

Number of comparisons? Number of swaps?

slide-39
SLIDE 39
  • Selection Sort

Number of comparisons?

  • Number of swaps?