Question

In: Computer Science

Problem 3 (5 marks). Suppose elements a[i] and a[i+k] are in the wrong order and we...

Problem 3 . Suppose elements a[i] and a[i+k] are in the wrong order and we swap them. Prove that this will remove at least 1 inversion but at most 2k − 1 inversions. (This is textbook problem 7.3.) Further explain why both the lower bound of 1 and the upper bound of 2k − 1 can be attained for any i, k, where k > 0.

Solutions

Expert Solution

The inversion that existed between A[i]A[i] and A[i+k]A[i+k] is removed. This shows at least one inversion is removed. Now let's consider A[i],A[i+1],…,A[i+k−1],A[i+k]A[i],A[i+1],…,A[i+k−1],A[i+k], Suppose A[i]A[i] is greater than A[i+1],…,A[i+k]A[i+1],…,A[i+k] and A[i+k]A[i+k] is smaller than A[i],…,A[i+k−1]A[i],…,A[i+k−1]. In this case, by swapping A[i]A[i] and A[i+k]A[i+k], we fix 2k−12k−1 inversions (−1−1 is that A[i]A[i] greater than A[i+k]A[i+k] and A[i+k]A[i+k] smaller than A[i]A[i] points to the same inversion).

Another way to think about 2k−12k−1 is that for each of the k−1k−1 elements A[i+1],A[i+2],…,A[i+k−1]A[i+1],A[i+2],…,A[i+k−1], at most two inversions can be removed by exchange. For instance, for A[i+1]A[i+1], two inversions are A[i]A[i] and A[i+1]A[i+1], and A[i+1]A[i+1] and A[i+k]A[i+k] (i.e. for sequence 10,4,3, by swapping 10 and 3, we remove inversion {10,4} and {4,3}). Thus, a maximum of 2(k−1)+1=2k−12(k−1)+1=2k−1.


Related Solutions

Suppose we have an Economic Order Quantity (EOQ) problem with discounts. A (annual demand)= 10000, k...
Suppose we have an Economic Order Quantity (EOQ) problem with discounts. A (annual demand)= 10000, k (order cost)= 10, c1 (item cost)= 8, h (holding cost)= 0.1 For orders of 2000 or more, the cost is discounted from 8 to 7.3728. What is the optimal TAC (total annual cost, including order and holding, and also including the purchase costs)?
Problem 5: Find Smallest Elements In this problem, we will write a function to find the...
Problem 5: Find Smallest Elements In this problem, we will write a function to find the smallest elements of a list. Define a function named find_smallest() that accepts two parameters: x and n. The parameter x is expected to be a list of values of the same time, and n is expected to be an either integer, or the value None, and should have a default value of None. • If n is set to None, then the function should...
I just need 3 and 5. I am not sure what I am doing wrong. I...
I just need 3 and 5. I am not sure what I am doing wrong. I get different numbers every time. Superior Markets, Inc., operates three stores in a large metropolitan area. A segmented absorption costing income statement for the company for the last quarter is given below: Superior Markets, Inc. Income Statement For the Quarter Ended September 30 Total North Store South Store East Store Sales $ 4,800,000 $ 960,000 $ 1,920,000 $ 1,920,000 Cost of goods sold 2,640,000...
3) We have not forgotten Halmos Pal. In class I asked you what’s wrong with this...
3) We have not forgotten Halmos Pal. In class I asked you what’s wrong with this “proof” of Halmos’ that all horses are the same color? It’s time to tell me what you found. (Try the web.)
Question 5: Hypothesis Testing: Each question is worth 7.5 marks: Total (15 marks) A. Suppose we...
Question 5: Hypothesis Testing: Each question is worth 7.5 marks: Total A. Suppose we know the standard deviation of the population is 3.7. We have a sample size of 625. We also have a sample mean of 46. We also have a population mean of 43. We want to test the hypothesis for the sample mean with 78% confidence level and do a two-tailed test on the hypothesis. State the null and alternate hypothesis for the sample mean and then...
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an...
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an input of n numbers stored in an array A[1:n]. Compare the following strategies in terms of their exact number of comparisons. Strategy 1: adapt the two separate For loops idea for minimum and maximum. Strategy 2: Run mergesort to sort the numbers in ascending or descending order, then output the 2nd ranked and (n-1)th ranked elements. First write each algorithm in pseudocde, then analyze...
I did most of this problem theres only a part that I keep getting wrong or...
I did most of this problem theres only a part that I keep getting wrong or not getting. I need help figuring out the blank boxes. that says Purchasing and Power and Question 2. Rest is done Sequential Method Jasmine Company manufactures both pesticide and liquid fertilizer, with each product manufactured in separate departments. Three support departments support the production departments: Power, General Factory, and Purchasing. Budgeted data on the five departments are as follows: Support Departments Producing Departments Power...
Problem 5-3 Order Books (LO2, CFA1) The following order book exists for a particular stock. The...
Problem 5-3 Order Books (LO2, CFA1) The following order book exists for a particular stock. The last trade on the stock was at $55.63. Buy Orders Sell Orders Shares Price Shares Price 250 $55.62 100 $55.65 250 55.61 600 55.66 900 55.60 1,000 55.68 150 55.58 850 55.69 600 55.70 a.  If you place a market buy order for 100 shares, at what price will it be filled? (Round your answer to 2 decimal places.)                  b. If you place a...
can someone tell me if i am wrong on any of these???? THANKS In order to...
can someone tell me if i am wrong on any of these???? THANKS In order to be able to deliver an effective persuasive speech, you need to be able to detect fallacies in your own as well as others’ speeches. The following statements of reasoning are all examples of the following fallacies: Hasty generalization, mistaken cause, invalid analogy, red herring, Ad hominem, false dilemma, bandwagon or slippery slope. 1. __________bandwagon fallacy_______ I don’t see any reason to wear a helmet...
Assume i = 4, j = 9, k = 5 and m = -3. What does...
Assume i = 4, j = 9, k = 5 and m = -3. What does each of the following statements print? a) printf("%d", i == 4); b) printf("%d", j!= 3); c) printf("%d", i >= 5 && j < 2); d) printf("%d", !m || k > m); e) printf("%d", !k && m); f) printf("%d", k - m < j || 5 - j >= k); g) printf("%d", j + m <= i && !0); h) printf("%d", !(j + m)); i)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT