example { 1 –11 50 6 8 –1} use bubble sort (sort in increasing order} after first pass {-11 1 6...

21

Upload: meryl-terry

Post on 04-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 2: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 3: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 4: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 5: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 6: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

Example

{ 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order}

After first pass

{-11 1 6 8 –1 50}

After Second Pass

{-11 1 6 -1 8 50}

After Third Pass

{-11 1 -1 6 8 50}

After Fourth Pass

{-11 -1 1 6 8 50}

After Fifth Pass

{-11 -1 1 6 8 50}

Done

Page 7: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 8: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

Example

{ 1 –11 50 6 8 –1} Use Insertion Sort (sort in increasing order}

After first pass

{-11 1 50 6 8 -1}

After Second Pass

{-11 1 50 6 8 -1}

After Third Pass

{--11 1 6 50 8 -1}

After Fourth Pass

{- 11 1 6 8 50 -1}

After Fifth Pass

{-11 -1 1 6 8 50}

Done

Page 9: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 10: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

Example

{ 1 –11 50 6 8 –1} Use Selection Sort (sort in increasing order}

After first pass (

{-11 1 50 6 8 -1}

After Second Pass

{-11 -1 50 6 8 1} ( {-11 –1 6 50 8 1}, {-11 –1 1 50 8 6 }

After Third Pass

{--11 -1 1 50 8 6}

After Fourth Pass

{- 11 -1 1 6 50 8}

After Fifth Pass

{-11 -1 1 6 8 50}

Done

Page 11: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 12: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 13: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 14: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

An example of greedy algorithm:

Solution is in steps and every step you pick the best solution for that step.

Page 15: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 16: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 17: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

Compare your solution (using the greedy algorithm) with the best solution (the one gives you the fewest number of coins).

Both will have the same number of quarters ….. (# of your quarters >= # of best solution’s quarters). If the inequality is strict, then # of coins in the best solution will be greater than yours ….

From so the changes in dimes, nickels and pennies in both solutions are the same (the remainder of the change divided by 25c). So both solution will have the same number of coins.

Suppose now you only use quarters, dims and pennies. Do you think the greedy algorithm work?

What is the result of the greedy algorithm for 30c?

Is this the optimal solution? (The fewest coins)?

Why?

Page 18: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 19: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

Halting Problem

• Does there exist a procedure (algorithm or program) that can tell a given program with a given input will stop (halt) after a finite number of steps.

• A Fundamental and interesting (theoretical) problem in computer science.

Page 20: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After
Page 21: Example { 1 –11 50 6 8 –1} Use Bubble Sort (sort in increasing order} After first pass {-11 1 6 8 –1 50} After Second Pass {-11 1 6 -1 8 50} After

More Specifically:

Let H(P, Q) be the halting program (it returns two values: halt and loop forever).

Define a new program K, taking input P(any program) K(P)

K(P) will loop forever if H(P, P) return “halt”

K(P) will halt if H(P, P) return “loop forever”

Then, H(P, Q) does not work for K.

If H(K, K) return halt, by definition K(K) will loop forever

If H(K, K) return loop forever, by definition K(K) will halt.