Download presentation
Presentation is loading. Please wait.
Published byRoland Nash Modified over 6 years ago
1
xguo9@student.gsu.edu Xuan Guo
Lab 6 Xuan Guo
2
Contents Section 3.1 Section 3.2 Section 3.3 Question 3, 5, 7, 35, 39
3
Question 3 section 3.1 Devise an algorithm that finds the sum of all the integers in a list.
4
Question 3 section 3.1 Devise an algorithm that finds the sum of all the integers in a list. (call the procedure AddEmUp, mimic the structure of Algorithm 1, the list is not empty) Input: a list of integers
5
Question 3 section 3.1 Devise an algorithm that finds the sum of all the integers in a list. Procedure AddEmUp(a1, ..., an: integers) sum := a1 for i :=2 to n sum := sum +ai return sum
6
Question 5 Section 3.1 Describe an algorithm that takes as input a list of n integers in non decreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is non decreasing if each integer in the list is at least as large as the previous integer in the list.)
7
Question 5 Section 3.1 Describe an algorithm that takes as input a list of n integers in non decreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is non decreasing if each integer in the list is at least as large as the previous integer in the list.) Find the cases when ai=ai+1 Need to skip repeated duplicates form and return a list
8
Question 5 Section 3.1 procedure duplicates (a1, a2, ..., an: integers in non decreasing order) k:= 0 {this counts the duplicates} j:= 2 while j≤n if aj =aj−1 then k:= k+1 ck := aj while j≤n and aj = ck j:= j+1 {c1, c2, ..., ck is the desired list}
9
Question 7 Section 3.1 Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list.
10
Question 7 Section 3.1 Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list. (Go through the list, and record the index of the last even integer seen)
11
Question 7 Section 3.1 Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list. procedure last even location(a1, a2, , an: integers) k := 0 for i := 1 to n if ai is even then k := i return k {k = 0 if there are no evens}
12
Question 35 Section 3.1 Use the bubble sort to sort 3, 1, 5, 7, 4, showing the lists obtained at each step.
13
Question 35 Section 3.1 Use the bubble sort to sort 3, 1, 5, 7, 4, showing the lists obtained at each step. Four passes 1) 1, 3, 5, 4, 7 2) 1, 3, 4, 5, 7 3) 1, 3, 4, 5, 7 4) 1, 3, 4, 5, 7
14
Question 39 Section 3.1 Use the insertion sort to sort the list in Exercise 35, showing the lists obtained at each step.
15
Question 39 Section 3.1 Use the insertion sort to sort the list in Exercise 35, showing the lists obtained at each step. 1) inserts 1, get 1, 3, 5, 7, 4 2) 5 inserted into 1, 3, get 1, 3, 5, 7, 4 3) 7 inserted into 1, 3, 5, 4) 4 is inserted
16
Question 9 Section 3.2 how that x2+4x+17 is O(x3)
but that x3 is not O(x2+4x+17).
17
Question 9 Section 3.2 how that x2+4x+17 is O(x3) but that x3 is not O(x2+4x+17). Proof: On the one hand we have x2+4x+17 <= x2+x2+x2 = 3x2 <= 3x3 for all x > 17, so x2+4x+17 is O(x3) with witness C = 3 and k = 17.
18
Question 9 Section 3.2 how that x2+4x+17 is O(x3) but that x3 is not O(x2+4x+17). Proof: On the one hand we have x2+4x+17 <= x2+x2+x2 = 3x2 <= 3x3 for all x > 17, so x2+4x+17 is O(x3) with witness C = 3 and k = 17. On the other hand, if x3 were O(x2+4x+17), then we would have x3 <= C(x2+4x+17) <= 3Cx2 for all sufficiently large x. But this says than x <= 3C, clearly impossible for the constant C to satisfy for all large x. Therefore x3 is not O(x2+4x+17).
19
Question 27 Section 3.2 Give a big-O estimate for each of these functions. For the function g in your estimate that f(x) is O(g(x)), use a simple function g of the smallest order. a) nlog(n2+1)+n2logn b)(nlogn+1)2+(logn+1)(n2+1)
20
Question 27 Section 3.2 Give a big-O estimate for each of these functions. For the function g in your estimate that f(x) is O(g(x)), use a simple function g of the smallest order. a) nlog(n2+1)+n2logn O(n2logn) logn2 = 2logn
21
Question 27 Section 3.2 Give a big-O estimate for each of these functions. For the function g in your estimate that f(x) is O(g(x)), use a simple function g of the smallest order. b)(nlogn+1)2+(logn+1)(n2+1) O(n2(logn)2) throw away the smaller order term
22
Question 13 Section 3.3 The conventional algorithm for evaluating a polynomial anxn+an−1xn−1+ ··· +a1x+a0 at x=c can be expressed in pseudocode by procedure polynomial(c, a0, a1, ..., an: real numbers) power:=1 y:=a0 for i :=1 to n power := power ∗ c y := y + ai ∗ power return y{y=ancn+an−1cn−1+ ··· +a1c+a0} where the final value of y is the value of the polynomial at x=c. a) Evaluate 3x2+x+1 at x=2 by working through each step of the algorithm showing the values assigned at each assignment step. b)Exactly how many multiplications and additions are used to evaluate a polynomial of degree n at x=c?(Do not count additions used to increment the loop variable.)
23
Question 13 Section 3.3 procedure polynomial(c, a0, a1, ..., an: real numbers) power:=1 y:=a0 for i :=1 to n power := power ∗ c y := y + ai ∗ power return y {y=ancn+an−1cn−1+ ··· +a1c+a0} where the final value of y is the value of the polynomial at x=c. a) Evaluate 3x2+x+1 at x=2 n = 2, a1 = 1, a2 = 3, and c = 2
24
Question 13 Section 3.3 procedure polynomial(c, a0, a1, ..., an: real numbers) power:=1 y:=a0 for i :=1 to n power := power ∗ c y := y + ai ∗ power return y{y=ancn+an−1cn−1+ ··· +a1c+a0} where the final value of y is the value of the polynomial at x=c. b)Exactly how many multiplications and additions are used to evaluate a polynomial of degree n at x=c?(Do not count additions used to increment the loop variable.) 2 multiplication and 1 addition per pass 2n multiplications and n additions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.