Related questions
Concept explainers
Consider the Sort-and-Count
Suppose that the initial list is:
92 71 36 91 27 48 14 34 81 26 24 65 78 51 37 22
Sort-and-Count makes two recursive calls. The first recursive call inputs the first half of the initial list:
92 71 36 91 27 48 14 34
and returns the sorted version of the first half, as well as the number of inversions found in the first half (22). The second recursive call inputs the second half of the initial list:
81 26 24 65 78 51 37 22
and returns the sorted version of the second half, as well as the number of inversions found in the second half (19). Sort-and-Count then calls Merge-and-Count. To Merge-and-Count, Sort-and-Count passes the sorted versions of the two halves of the original list:
14 27 34 36 48 71 91 92, and
22 24 26 37 51 65 78 81
Merge-and-Count begins merging the two half-lists together, while counting inversions.
14 27 34 36 48 71 91 92 22 24 26 37 51 65 78 81
14 22 24 26 27 34 36 37 48 51 65 71 78 81 91 92
7 7 7 4 3 3 2 2 (inversions)
The inversions counted during the merge are shown in the last line above. The total number of inversions counted between the two sorted halves is
7+たす7+たす7+たす4+たす3+たす3+たす2+たす2 =わ 35.
Therefore the total number of inversions in the original list is
22+19+35 = 76.
YOUR ASSIGMENT:
Do the same work as above, except starting with this initial list:
26 81 87 32 66 72 86 97 23 48 14 71 89 18 49 62
Trending nowThis is a popular solution!
Step by stepSolved in 6 steps with 5 images
- Python Using recursion only! No loops or global var! return the largest number not print! Using such len(), isdigit() def getMaxDigit(phrase):arrow_forwardThis is a MATLAB Coding problem: Implement a recursive function, called count, that takes two inputs, an array of integers and target integer. count should search the array for the target integer and return the number of occurrences of the target integer in the array. For example: disp(count([1, 2, 4, 1, 2, 1], 1)) % Generates 3disp(count([1, 2, 4, 1, 2, 1], 2)) % Generates 2disp(count([1, 2, 4, 1, 2, 1], 4)) % Generates 1disp(count([1, 2, 4, 1, 2, 1], 9000)) % Generates 0arrow_forwardNo plagarism please! Correct and detailed answer will be Upvoted else downvoted. Thank you!arrow_forward
- Write a recursive implementation of Euclid's Algorith for finding the greatest common divisor(GCD) of two intergers. Descriptions of this algorithm are available in algebra books and on theweb. (Note: A nonrecursive version of the GCD problem was given in the programming exercisesfor Chapter 7.) Write a test program that calls your GCD procedure five times, using thefollowing pairs of integers: (5,20),(24,18),(11,7),(432,226),(26,13). After each procedure call,display the GCD.arrow_forwardDesign and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.arrow_forwardGiven a list of integers, we want to know whether it is possible to choose a subset of some of the integers, such that the integers in the subset adds up to the given sum recursively. We also want that if an integer is chosen to be in the sum, the integer next to it in the list must be skipped and not chosen to be in the sum. Do not use any loops or regular expressions. Test cases: skipSum([2, 5, 10, 6], 12) true skipSum([2, 5, 10, 6], 7) false skipSum([2, 5, 10, 6], 16) false Given code: public static boolean skipSum (List list, int sum) { // call your recursive helper method return skipSumHelper (list, e, sum); 1. 2. 3. 4.arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education