4. Suppose that you are given an array A[0..n-1] of numbers, and an index i 0...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
4. Suppose that you are given an array A[0..n-1] of numbers, and an index "i" 0<=i<n-1. The index "i" implicitly divides the array into two parts A[0..i] and A[i..n-1]. Design an in-place algorithm swap that swaps these two subarrays without changing the order of the elements. As an example, assume you are given the following numbers: A = [10, 2, 4, 8, 9, 3, 15, 6] and index "i" = 2. Then after the swap operation, the array would turn into: A = [8, 9, 3, 15, 6, 10, 2, 4]. void swap (vector<int> &A, int i) You must write a recursive auxiliary function to solve this problem, i.e., swapAux(A, 0, i, n-1); The following figure illustrates the 3 recursive cases you will encounter: leftSize == rightSize 9 3 15 6 10 2 4 8 1024893 15 6 10 2 10 2 4 8 8 9 3 15 6 3 15 6 leftSize rightSize 8 9 3 10 2 15 6 leftSize > rightSize 10 23 15 6 4 8 9 a. When leftSize== rightSize, we simply do pairwise swaps, and we are done. b. If the leftSize < rightSize, then we swap the first 2* leftSize elements as shown. We are now left with n-leftSize elements to process. c. If leftSize > rightSize, then we swap the last rightSize elements as shown. We are now left with m- rightSize elements to process. Implement this recursive function in C++. Since this is a tail-recursive function, you can turn it into an iterative algorithm. Also try implementing iterative algorithm. 4. Suppose that you are given an array A[0..n-1] of numbers, and an index "i" 0<=i<n-1. The index "i" implicitly divides the array into two parts A[0..i] and A[i..n-1]. Design an in-place algorithm swap that swaps these two subarrays without changing the order of the elements. As an example, assume you are given the following numbers: A = [10, 2, 4, 8, 9, 3, 15, 6] and index "i" = 2. Then after the swap operation, the array would turn into: A = [8, 9, 3, 15, 6, 10, 2, 4]. void swap(vector<int> &A, int i) You must write a recursive auxiliary function to solve this problem, i.e., swapAux(A, 0, i, n-1); The following figure illustrates the 3 recursive cases you will encounter: leftSize == rightSize 9 3 15 6 10 2 4 8 1024893 15 6 10 2 10 2 4 8 8 9 3 15 6 3 15 6 leftSize rightSize 8 9 3 10 2 15 6 leftSize » rightSize 10 23 15 6 4 8 9 a. When leftSize == rightSize, we simply do pairwise swaps, and we are done. b. If the leftSize< rightSize, then we swap the first 2*leftSize elements as shown. We are now left with n-leftSize elements to process. c. If leftSize > rightSize, then we swap the last rightSize elements as shown. We are now left with m- rightSize elements to process. Implement this recursive function in C++. Since this is a tail-recursive function, you can turn it into an iterative algorithm. Also try implementing iterative algorithm. 4. Suppose that you are given an array A[0..n-1] of numbers, and an index "i" 0<=i<n-1. The index "i" implicitly divides the array into two parts A[0..i] and A[i..n-1]. Design an in-place algorithm swap that swaps these two subarrays without changing the order of the elements. As an example, assume you are given the following numbers: A = [10, 2, 4, 8, 9, 3, 15, 6] and index "i" = 2. Then after the swap operation, the array would turn into: A = [8, 9, 3, 15, 6, 10, 2, 4]. void swap (vector<int> &A, int i) You must write a recursive auxiliary function to solve this problem, i.e., swapAux(A, 0, i, n-1); The following figure illustrates the 3 recursive cases you will encounter: leftSize == rightSize 9 3 15 6 10 2 4 8 1024893 15 6 10 2 10 2 4 8 8 9 3 15 6 3 15 6 leftSize rightSize 8 9 3 10 2 15 6 leftSize > rightSize 10 23 15 6 4 8 9 a. When leftSize== rightSize, we simply do pairwise swaps, and we are done. b. If the leftSize < rightSize, then we swap the first 2* leftSize elements as shown. We are now left with n-leftSize elements to process. c. If leftSize > rightSize, then we swap the last rightSize elements as shown. We are now left with m- rightSize elements to process. Implement this recursive function in C++. Since this is a tail-recursive function, you can turn it into an iterative algorithm. Also try implementing iterative algorithm. 4. Suppose that you are given an array A[0..n-1] of numbers, and an index "i" 0<=i<n-1. The index "i" implicitly divides the array into two parts A[0..i] and A[i..n-1]. Design an in-place algorithm swap that swaps these two subarrays without changing the order of the elements. As an example, assume you are given the following numbers: A = [10, 2, 4, 8, 9, 3, 15, 6] and index "i" = 2. Then after the swap operation, the array would turn into: A = [8, 9, 3, 15, 6, 10, 2, 4]. void swap(vector<int> &A, int i) You must write a recursive auxiliary function to solve this problem, i.e., swapAux(A, 0, i, n-1); The following figure illustrates the 3 recursive cases you will encounter: leftSize == rightSize 9 3 15 6 10 2 4 8 1024893 15 6 10 2 10 2 4 8 8 9 3 15 6 3 15 6 leftSize rightSize 8 9 3 10 2 15 6 leftSize » rightSize 10 23 15 6 4 8 9 a. When leftSize == rightSize, we simply do pairwise swaps, and we are done. b. If the leftSize< rightSize, then we swap the first 2*leftSize elements as shown. We are now left with n-leftSize elements to process. c. If leftSize > rightSize, then we swap the last rightSize elements as shown. We are now left with m- rightSize elements to process. Implement this recursive function in C++. Since this is a tail-recursive function, you can turn it into an iterative algorithm. Also try implementing iterative algorithm.
Expert Answer:
Answer rating: 100% (QA)
The question provides an algorithmic challenge where one must swap two subarrays of a given array in C without altering the order of elements within t... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
BUS132 Project 6 You are the budget expert for R & S Manufacturing Company. The Master Budget and Actual Results for 2013 are listed below. Your general manger, however, wants a comparison based more...
-
Bob and Sally have three children and called you to review their life insurance needs, Based on the information below, calculated the amount of life insurance Bob requires. Sallys After-tax income...
-
Suppose that you are given an n n checkerboard and a checker. You must move the checker from the bottom edge of the board to the top edge of the board according to the following rule. At each step...
-
On October 1, 2024, Modern Company purchased a patent for $153,600 cash. Although the patent gives legal protection for 20 years, the patent is expected to be used for only eight years. Requirements...
-
When we use a normal distribution to approximate a binomial distribution, why do we make a continuity correction?
-
Data integrity and security is a particular issue in a database. For data to have integrity, it must be accurate, consistent and free from accidental corruption. Required: What controls are required...
-
Convert \(\frac{25}{6}\) to a mixed number. Do the indicated conversion. If it is a repeating decimal, use the correct notation.
-
Multiple Choice. Choose the best answer. 1. The FASBs objectives of financial reporting for not-for-profit organizations include all of the following except: a. Making resource allocation decisions....
-
Empire Electric Company (EEC) uses only debt and common equity.It can borrow unlimited amounts at an interest rate ofrd = 9% as long as it finances at its target capitalstructure, which calls for 3 2...
-
Formulate a linear program which can be used to generate a comprehensive plan for the whole Far Eastern operations. Clearly define every variable used in your formulation. Start your report with an...
-
The last entry is utility expense. I couldn't fit it all in one picture. a. Received $12,200 cash for consulting services rendered in January. b. Issued common stock to investors for $16,000 cash. c....
-
The purpose of this reflection assignment is to use the experience of working in your group to explain some of the group concepts examined this week. Instructions Using the information from your...
-
What are some issues facing developing nations? What kind of assistance is the United States providing to developing nations? What is your assessment of the effectiveness of these actions? Should the...
-
EXPLAIN CASH FLOW CALCULATION ESTIMATE OF INCREMENTAL CASH FLOW Old Spinning Machine $ 9 6 4 $ 1 , 3 6 7 $ 1 , 4 0 3 $ 1 , 4 4 0 $ 1 , 4 7 8 $ 1 , 5 1 7 $ 1 , 3 7 8 $ 1 , 4 1 9 $ 1 , 4 6 2 $ 1 , 5 0...
-
What are two examples where your business demonstrates ethical practice and/ or social responsibility that could be applied to marketing and sales principles and processes. and reasoning. and two...
-
How can cross-functional collaboration and knowledge sharing facilitate the dissemination of best practices and standardized processes, promoting a culture of excellence and continuous improvement...
-
JaiLai Companys stock has a beta of 0.6, the current risk-free rate is 6.4 percent, and the expected return on the market is 12 percent. What is JaiLai's cost of equity? Note: Round your answer to 2...
-
Do animals have rights? If so, what are they? What duties do human beings have toward animals? Does KFC protect animal welfare at an acceptable level?
-
Let p be an odd prime. A number a ? Z * p is a?quadratic residue?if the equation?x 2 =?a (mod?p)?has a solution for the unknown?x. a.?Show that there are exactly?(p???1)/2?quadratic residues,...
-
Why do we analyze the expected running time of a randomized algorithm and not its worst-case running time?
-
Using Figure 10.2 as a model, illustrate the result of each operation in the sequence ENQUEUE?(Q, 4), ENQUEUE?(Q, 1), ENQUEUE?(Q, 3), DEQUEUE?(Q), ENQUEUE?(Q, 8), and DEQUEUE?(Q)?on an initially...
-
Explain the six-legged stool of the financial reporting process.
-
What are the three M's of financial reporting fraud?
-
How did Sunbeam create revenues?
Study smarter with the SolutionInn App