Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms (continued)

Similar presentations


Presentation on theme: "Algorithms (continued)"— Presentation transcript:

1 Algorithms (continued)

2 Pseudocode Set i = 1 While i < 10
1. 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 this code and see what happens. Set up boxes for num, sum, x. Use data 3, 5, 2, 8, 7 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 Output a What would effect be if we switched the two assignment statements in step 2.?

5 stepping through code for understanding for debugging a b q r
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 Output a for understanding for debugging a b q r 42 15 ? Initial values 42 15 2 12 after step 1 15 12 2 after step 2 15 12 1 3 step 1 (2nd time) 12 3 1 step 2 (2nd time) 12 3 4 step 1 (3rd time) 3 4 step 2 (3rd time)

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) Linear_search (x) i = 0 WHILE i < n+1 IF M(i) = x then halt and output i ELSE i = i+1 Output “not in the list”. Example use: suppose we had UINs stored in memory, each followed by a phone number. Goal is to find phone number of person with given UIN. How many steps does it take when Linear_search is run?

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 THEN move left finger to midpoint ELSE move right finger to midpoint set midpoint halfway between fingers END WHILE

8 Binary Search Assume sorted data M(0) … M(n) Binsearch (x) left = 0
right = n midpoint = (left + right) / 2 WHILE x notequal M(midpoint) IF M(midpoint) < x THEN left = midpoint ELSE right = midpoint END WHILE Give warning about left+right/2 need to round/truncate, and careful consideration of ending condition…. might need to end when left and right are one apart. This actually has several bugs. Can you figure out what they are?

9 stepping through binary search
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 left right midpoint searching for 33 16 ? Initial values 16 8 compute midpt 8 16 move left 8 16 12 compute midpt 8 12 move right 8 12 10 compute midpt 8 10 move right 8 10 9 compute midpt

10 Running Times Binary Search At each iteration, range is cut in half
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” Yes, this is just (base 2) log N Searching among 2300 elements takes only 300 steps (this exceeds the number of particles in the universe!!) Ask class to determine number of queries for input of size 1024.

11 Linear search vs. Binary search

12 Sorting Algorithms Different methods to put things in order
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 Etc., for passes ending at n-2, n-3, etc. Do bubble sort with volunteers, based on height.

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

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

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 make a pass up to position max Let max = n-1 blah blah blah Let max = n-2 … etc Let max = 2 Let max = 1 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

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 make a pass up to position max 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 max = 1 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

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

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

20 A presidential message

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

22 Insertion Sort FOR i = 1 to n /* Insert M(i) into correct place in new list T() { j = /* Start looking at position T(1) WHILE M(i) < T(j) { 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)… T(j+1) = M(i) where slide is a separate “procedure” that we have written.

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

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

25 Quick Sort 1 5 9 4 7 Pick a “pivot’ at random 2 8 3 6 1 4 5 9
Compare each element to the pivot, placing it to left or right 7 2 8 3 6 Pivot is in correct place. Now sort the left, right using Quick Sort method…..

26 Quick Sort 1 4 5 9 Pick a pivot at random 7 2 8 3 6
Compare each element to the pivot, placing it to left or right 1 2 3 4 7 9 5 6 8 Pivot is in correct place. Now sort the left, right using Quick Sort method…..

27 Quick Sort 1 4 9 5 7 2 6 3 8 5 6 8 9 4 7 3 1 2 etc... 3 4 5 6 7 8 9 1 2

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 n2 n log n n log n

30 Quick calculations Sort n=1,000,000,000 items with algorithms taking times n2, 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? Demo: sorting applets: Demo: asSORTed dances:

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


Download ppt "Algorithms (continued)"

Similar presentations


Ads by Google