Fill This Form To Receive Instant Help
Homework answers / question archive / Consider the following meaningless algorithm: // PRE: a,b,c are integers if a + b > c c = a + b end if a + b == c c = c^2 end if a + b <= c print(’hi!’) end Suppose there are certain time requirements for various operations: Addition takes 2 seconds
Consider the following meaningless algorithm:
// PRE: a,b,c are integers if a + b > c c = a + b end if a + b == c c = c^2 end if a + b <= c print(’hi!’) end |
Suppose there are certain time requirements for various operations:
Solution:
Solution:
Solution:
Solution:
Define f(n) = 5n2 + 7nlgn − lgn + 10.
Solution:
Solution:
Suppose that for an input of size n: [10 pts]
Plot these two functions and indicate the approximate intersection point. For which n is each algorithm faster? Explain how you came up with your values. Use of computational tools is permitted for this question provided you explain which tools you used and how you used them.
Plots may be basic and hand-drawn.
Solution:
Give an example of a list for which Binary Search finds the target element of 17 on exactly [10 pts] the third comparison. Do not count the while loop check as a comparison. Give both the list and then at each step give the value of C, what the result of the comparison is, and how the comparisons update L or R.
Solution:
Backstory: Suppose you have a list A[0,...,n-1] with n ≥ 10 and you wish to find the five [15 pts] largest and five smallest entries. One obvious approach would be to sort the data and then just select the first five and last five entries. However certain external constraints may force your algorithm to stop early and as such you want to ensure that it takes care of the two ends of the list first. None of our sorting algorithms do this.
Your Job: Modify SelectionSort so it alternates between the left and right sides of the list, starting on the left. In other words it gets A[0] correct, then A[n-1], then A[1], then A[n-2], and so on, until the list is sorted. Write the pseudocode for this. You may assume n is even.
Solution:
Find integral-related bounds for the following. The terms are decreasing, you do not need to [10 pts] prove this.
Solution:
Consider the following pseudocode. Assume n is even and the list A[0,...,n-1] contains only 1s and 2s. For example A might be [1,2,2,1,1,2,1,1].
sum = 0 result = 1 for i = 0 to n-1 sum = sum + A[i] for j = i to n-1 result = result * A[j] end if sum == n break end end |
c1 c1 c2 c5
c3 c4
For this pseudocode, answer the following. These are asking for exact time requirements, not time complexity!
(a) Write down the expression (use sum notation) for the exact best-case time requirement. Do not evaluate this sum. Solution: |
[5 pts] |
(b) Write down the expression (use sum notation) for the exact worst-case time requirement. |
[10 pts] |
Evaluate this sum.
Solution:
NOTE: This question is challenging so do it last!
Suppose you have a list A[0,...,n-1] whose length is a positive multiple of 5 and you wish [15 pts] to make it cumulative, meaning you want to:
This should be pretty simple but suppose you cannot access the entries in A directly. Instead you have a separate working memory R[0,...,4] which you can access directly. You also have the command in(i) which copies A[i,...,i+4] to R[0,...,4] and out(i) which does the reverse. These are called block transfers.
Due to hardware limitations neither command called with some i may spill over the end of A.
Write the pseudocode for this. Full credit will be granted if you minimize the number of block transfers but majority of credit will be granted even if you don’t.
Solution:
9. Extra Credit:
Write a haiku related to your favorite algorithm we covered in class. In English a haiku is a [≤ 3 pts] poem which follows a 5-7-5 syllable scheme. This is worth up to 3 extra credit points.
Already member? Sign In