Download presentation

Presentation is loading. Please wait.

Published byReanna Mell Modified about 1 year ago

1
Algorithms (continued)

2
Pseudocode Set i = 1 While i < print [i “bottles of beer on a stone”] 2. print [i “bottles of beer”] 3. print [“if one of those bottles should happen to clone”] 4. print [i+1 “bottles of beer on a stone”] 5. INCREMENT i (i = i+1 or “i++”)

3
Pseudocode Set num = 0, set sum = 0 While there are more numbers to be read 1. read(x) (let x = next number read) 2. sum = sum + x (increment sum by x) 3. num++ (increment num by 1) Output: sum/num walk through code with data = 3, 5, 2, 8, 7

4
Pseudocode GCD (a, b) While remainder r is not 0 1. find q, and r < b, so that a=bq+r 2. set a = b set b = r set b = r Output a

5
stepping through code GCD (a, b) While remainder r is not 0 1. find q and r < b so that a=bq+r so that a=bq+r 2. set a = b set b = r set b = r Output a abqr 4215?? Initial values after step after step step 1 (2nd time) step 2 (2nd time) step 1 (3rd time) 3040 step 2 (3rd time) for understanding for debugging

6
Linear Search Assume data M(0) … M(n) Input x; Find memory location i such that M(i)=x (or report that x is not in list) How many steps does it take when Linear_search is run? Linear_search (x) i = 0 WHILE i < n+1 IF M(i) = x then halt and output i IF M(i) = x then halt and output i ELSE i = i+1 Output “not in the list”.

7
Binary Search Assume sorted data M(0) … M(n) Binsearch (x) put left, right fingers on 0 and n let midpoint be the point halfway between. WHILE x not equal M(midpoint) IF M(midpoint) < x IF M(midpoint) < x THEN move left finger to midpoint THEN move left finger to midpoint ELSE move right finger to midpoint ELSE move right finger to midpoint set midpoint halfway between fingers set midpoint halfway between fingers END WHILE END WHILE

8
Binary Search Assume sorted data M(0) … M(n) Binsearch (x) left = 0 right = n midpoint = (left + right) / 2 midpoint = (left + right) / 2 WHILE x notequal M(midpoint) IF M(midpoint) < x THEN left = midpoint ELSE right = midpoint midpoint = (left + right) / 2 END WHILE END WHILE This actually has several bugs. Can you figure out what they are?

9
stepping through binary search 016? Initial values 8168 move left compute midpt 8 12 move right compute midpt 810 move right leftrightmidpoint 0168 compute midpt ARRAY M( ) M(0) = 3 M(1) = 4 M(2) = 6 M(3) = 21 M(4) = 24 M(5) = 27 M(6) = 28 M(7) = 30 M(8) = 31 M(9) = 33 M(10)= 39 M(11)= 44 M(12)= 45 M(13)= 48 M(14)= 50 M(15)= 55 M(16)= 57 searching for compute midpt

10
Running Times Binary Search At each iteration, range is cut in half At each iteration, range is cut in half Total number of iterations to find an item among N elements is y such that Total number of iterations to find an item among N elements is y such that “cut N in half y times to get down to 1” “cut N in half y times to get down to 1” Yes, this is just (base 2) log N Yes, this is just (base 2) log N Searching among elements takes only 300 steps Searching among elements takes only 300 steps (this exceeds the number of particles in the universe!!) (this exceeds the number of particles in the universe!!)

11
Linear search vs. Binary search

12
Sorting Algorithms Different methods to put things in order Different methods to put things in order Which are fastest methods? Which are fastest methods?

13
Bubble Sort Put left, right fingers on M(0), M(1) IF M(left) > M(right) THEN swap them Move left, right fingers to right IF right finger = n THEN done with this pass, start again BUT next time only go up to n-1 BUT next time only go up to n-1 Etc., for passes ending at n-2, n-3, etc.

14
Aside: FOR loops new kind of iteration command (similar to WHILE) new kind of iteration command (similar to WHILE) Example Example FOR i = 1 to 10 print 3*i print 3*i Example Example FOR count = 100 downto 1 print count print count Outputs 3,6,9,…,30 Outputs 100,99,98,…,1

15
Bubble Sort FOR max = n-1 downto 1 FOR left = 1 upto max right = left + 1 IF M(left) > M(right) THEN swap them LET’S UNROLL THIS CODE TO MAKE SURE WE UNDERSTAND make a pass up to position max

16
Bubble Sort FOR max = n-1 downto 1 FOR left = 1 upto max right = left + 1 IF M(left) > M(right) THEN swap them Let max = n-1 blah blah blah blah blah blah Let max = n-2 blah blah blah blah blah blah … etc Let max = 2 blah blah blah blah blah blah Let max = 1 blah blah blah blah blah blah There are n-1 phases During a phase, what happens? A nested FOR loop is executed The nested loop moves from 1 to max and does a swap if necessary Let’s look at an example with n = 4 make a pass up to position max

17
Bubble Sort FOR max = n-1 downto 1 FOR left = 1 upto max right = left + 1 IF M(left) > M(right) THEN swap them max = 3 IF M(1) > M(2) swap them IF M(2) > M(3) swap them IF M(3) > M(4) swap them max = 2 IF M(1) > M(2) swap them IF M(2) > M(3) swap them max = 1 IF M(1) > M(2) swap them IF M(1) > M(2) swap them Now we can see the effect of the inner loop for each “version” of the outer loop… …and the overall effect of the nested loops make a pass up to position max

18
Correctness Why does it work? Why does it work? After first pass, the largest is in position n After first pass, the largest is in position n 2 nd pass, the 2 nd largest is in position n-1 2 nd pass, the 2 nd largest is in position n-1 3 rd pass, the 3 rd largest is in position n-2 3 rd pass, the 3 rd largest is in position n-2 etc. etc. nth pass, the nth largest is in position 1 nth pass, the nth largest is in position 1

19
Running time Count comparisons Count comparisons Don’t get bogged down in detail Don’t get bogged down in detail Iteration is the biggest contributor Iteration is the biggest contributor Bubble Sort: (n-1) + (n-2) + … Bubble Sort: (n-1) + (n-2) + … = n(n-1)/2 = (n 2 -n)/2 = n 2 /2 - n/2 ≤ n 2 /2

20
A presidential message A presidential message A presidential message A presidential message

21
Insertion Sort Insert first Insert next into proper place etc. (typical method for sorting playing cards in hand)

22
Insertion Sort FOR i = 1 to n /* Insert M(i) into correct place in new list T() { j = 1 /* Start looking at position T(1) { j = 1 /* Start looking at position T(1) WHILE M(i) < T(j) WHILE M(i) < T(j) { j = j+1 { j = j+1 } /* AHA! M(i) belongs right after T(j) slide T(j+1), T(j+2), … over to T(j+2), T(j+3)… slide T(j+1), T(j+2), … over to T(j+2), T(j+3)… T(j+1) = M(i) T(j+1) = M(i) } where slide is a separate “procedure” that we have written.

23
Running Times Insertion Sort: … + n-1 ≤ n 2 /2 (see previous analysis) ≤ n 2 /2 (see previous analysis)

24
Quick Sort A very fast method in practice A very fast method in practice Used often, especially with large data sets Used often, especially with large data sets

25
Quick Sort Pick a “pivot’ at random Compare each element to the pivot, placing it to left or right Pivot is in correct place. Now sort the left, right using Quick Sort method…..

26
Quick Sort Compare each element to the pivot, placing it to left or right Pivot is in correct place. Now sort the left, right using Quick Sort method….. Pick a pivot at random

27
Quick Sort etc

28
Quick Sort Quick Sort (in-class demo): depends on how lucky you are with “pivot” complex analysis shows on average, you are lucky running time on average can be shown to be about n log n

29
Running time matters n2n2 n log n n log n

30
Quick calculations Sort n=1,000,000,000 items with algorithms taking times n 2, and n log n, on computers capable of doing 1,000,000,000 (a billion) comparisons per second? Sort n=1,000,000,000 items with algorithms taking times n 2, and n log n, on computers capable of doing 1,000,000,000 (a billion) comparisons per second? How much time is taken by each algorithm? How much time is taken by each algorithm? Demo: sorting applets: Demo: sorting applets: Demo: asSORTed dances: Demo: asSORTed dances:

31
Summary pseudocode pseudocode stepping through code stepping through code basic algorithms: linear search, binary search, different sorting algorithms basic algorithms: linear search, binary search, different sorting algorithms notion of running time and computing approximate bounds to determine efficiency notion of running time and computing approximate bounds to determine efficiency

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google