Question

Consider the Sort-and-Count algorithm explained in section 5.3 of our text: "Counting Inversions"

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

Expert Solution

Want to see the full answer?

Check out a sample Q&A here
Blurred answer
Students who’ve seen this question also like:
C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
Not helpful? See similar books
marketing sidebar icon
Want to see this answer and more?
Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: Apply quicksort to sort the below list in alphabetical order. Draw the tree of the recursive calls…

A: Solution  Steps to solve the Quick Sort  Step 1 − Choose the highest index value has pivot Step 2 −…

Q: Write an algorithm that sorts a list of n items by dividing it into three sublists of about n/3…

A: Function sorting( array s[1..n]): //array to be sorted is passed of size n. If (n = 1): return s[n]…

Q: Write for the following problem a recursive algorithm whose worst-case time complexity is not worse…

A: PROGRAM : import java.util.*; import java.lang.*;   class main { ' static int max_diff(int[] arr,…

Q: Write the fourth values of the recursively-defined sequence SS.  S(1)=3S(1)=3…

A: Recursion is a process of calling the same function itself

Q: Write the pseudocode for a recursive algorithm to compute b3k, where b is a real number and k is a…

A: According to the Question below the Solution:

Q: Consider the following recursive algorithm: Int active(Int lst [1.int lowerindex, Int upperindex)…

A: As per our guidelines, we are supposed to answer only one question. Kindly repost the remaining…

Q: ) In sage, reducing an integer a modulo m is achieved with the expression a % m. The following sage…

A: The answer is

Q: A recursive algorithm similar to calculating a number in the Fibonacci sequence, where each state…

A: We know that the recursive equation for Fibonacci is T(n)=T(n-1)+T(n-2)+O(1).What this means is the…

Q: Suppose the following array were passed in to a mergesort algorithm: 42 39 87 21| 56 93 43 35 Draw…

A: I have applied the merge sort algorithm in the array and I have show how our array will be…

Q: Consider the following recursive algorithm fib, which takes as input an integer n > 0: fib(n): if n…

A: Answer: Correct answer is 8.  Because here call of Fib(99) then 99 is greater than 0 or 1 then run…

Q: This is a MATLAB Coding problem: Implement a recursive function, called count, that takes two…

A: Code: function index=count(A,b) %defining the function n=numel(A); %determining the size of array…

Q: 1. Suppose a recursive algorithm performs 2 recursive calls. Assume the first recursive call is of…

A: Given: *) Suppose a recursive algorithm performs 2 recursive calls. *) Assume the first recursive…

Q: People often use recursive algorithms. Discuss the difference between direct recursion and indirect…

A: First of all, let's understand what is Recursion. Recursion is a process in a programming language…

Q: A game is played by moving a marker ahead either 2 or 3 steps on a linear path. Let cn be the number…

A: GIVEN: A game is played by moving a marker ahead either 2 or 3 steps on a linear path. Let cn be…

Q: A game is played by moving a marker ahead either 2 or 3 steps on a linear path. Let cn be the number…

A: Answer : Pseudo Code in C++

Q: In Python, write a recursive implementation of Fibonacci without memoization. Include a timer to…

A: Python program to solve the given problem is below.

Q: Exercise 8.10.9: Recursively computing a number raised to an exponent that is a power of 2. About…

A: Algorithm to calculate r raised to the power 2 raised to the power n. We know r2n = r2.2n-1  =…

Q: I want to solve them as algorithms 1-Write a steps to search for a node contain a given value in a…

A: Start Store the first pointer in some temp Iterate till end of list If data is same as search,…

Q: I want to solve them as algorithms 1-Write a steps to search for a node contain a given value in a…

A: I am creating functions for the below operations which will take SLLL and required data as a…

Q: Define the following variant of the Fibonacci sequence called Fib3 using recursion : ●  fib3(0) = 0…

A: Below is the code in C++ as language is not mentioned and sample output:

Q: for i: = 1 to 4n S:= 0 for j:= 1 to i - 1 s:= s+j• (i - j+ 1) next j r:= s? next i (a) What is the…

A: LINE 1 -     for i := 1 to 2nLINE 2 -                s := 0LINE 3 -                for j := 1 to…

Q: IN PYTHON, USING RECURSION: ask the user for the length of a square and the character they want to…

A: Required: ask the user for the length of a square and the character they want to use. Draw the…

Q: Consider a sequence 3,5,7,8,9,12,15. For searching a key=5, how many recursive calls are made for…

A: Binary search algorithm is an algorithm for sorted sequence. The given sequence is 3,5,7,8,9,12,15…

Q: b. The reason that the recursive algorithm is so slow is because the algorithm used to simulate…

A: Clearly here time complexity is very high of order O(2^n) due to the multiple calls to the same…

Q: A game is played by moving a marker ahead either 2 or 3 steps on a linear path. Let ca be the number…

A: Given: Cn = Cn-2 + Cn-3 , C1=0, C2= 1,C3=1  We have to write the recursive algorithm to compute Cn .…

Q: Design and implement an algorithm using recursion and backtracking to sort an array of integers into…

A: Note: Since the language in the question is not defined so I am using python to implement this…

Q: A recursive algorithm is designed in such a way that it is divided into three subproblems of size in…

A: Introduction :Given , A recursive algorithm description, which divides the subproblem in size ratio…

Q: One of the most common examples of recursion is an algorithm to calculate the factorial of an…

A: The algorithm for the factorial is Factorial (int n)  Start if(n<=1) , go to step 3 else to step…

Q: RECURSIVE PYTHON The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are…

A: The answer is as follows:-

Q: A binary search only works if the values in the list are sorted. A bubble sort is a simple way to…

A: Bubble sort as well explained in the question, it is the comparison between the two successor…

Q: Part 1) Write pseudocode for an iterative algorithm which finds the maximum value of a list of…

A: Part 1:   Procedure Maximum (A=a1, a2,.....,an: integer list )   //Initialize Max to a1 1: Max = a1…

Q: Part 1) Write pseudocode for an iterative algorithm which finds the maximum value of a list of…

A: Part 1:   Procedure Maximum (A=a1, a2,.....,an: integer list )   //Initialize Max to a1 1. Max = a1…

Q: Part 1) Write pseudocode for an iterative algorithm which finds the maximum value of a list of…

A: Procedure Maximum (A=a1, a2,.....,an: integer list )   //Initialize Max to a1 1. Max = a1 //iterate…

Q: Is it possible to write a tail-recursive version of the classic quicksort algorithm? Why or why not?

A: Tail-recursion: Tail recursion is a form of recursion in which the recursive call occurs at the end…

Q: Find a longest common subsequence (LCS) of two DNA sequences and fill the following table using the…

A: Given data, S1= CGATAG S2= GCATA Remember: 1. The cell directly to the left of it 2. The cell…

Q: Write a recursive algorithm to multiply two positive integers m and n using repeated addition.…

A: Base case: Input is 0 Recursive case: When the input not equal to 0 and greater than 1.

Q: Consider the recursive algorithm below for computing the sum of the first n cubes: S(n) = 13 + 23 +…

A: Click to see the answer

Q: Explain the general plan for analyzing the efficiency of the non-recursive algorithm given below.…

A: General Plan for Analyzing the Time Efficiency of Non-recursive Algorithms Decide on a parameter…

Q: Consider the following recursive algorithm: Int relog(int num) if (num < 1) return 10%3; else return…

A: NOTE - As per our guideline we are supposed to solve one question from multiple questions, so please…

Q: Euclid's algorithm (or the Euclidean algorithm) is an algorithm that computes the greatest common…

A: Click to see the answer

Q: Print the square that consists of NxN cells filled with numbers from 1 to N*N in a spiral mode(see…

A: Given: Print the squares that consist of NxN cells filled with number form 1 to N*N in a spiral…

Q: The n-th Fibonacci number Fn is defined recursively as Fn=Fn−1+Fn−2 where F0=0 and F1=1 . For…

A: AREA            RESET,    CODE,    READONLY ENTRY start   MOV     R1, #16     ; n = 16           MOV…

Q: For the following problem please write an algorithm in plain English .i.e give details as to how you…

A: Algorithm to arrange the deck of 52 cards for the given scenario is as follows:

Q: A recursive sequence is defined by - d k = 6 d k − 1 + 3 , for all integers k ≥ 2 and d1 = 2 Use…

A: The above given question needed little correction as it is not satisfy the recursive function.…

Q: Q. Eliminate immediate left recursion and left factoring for the productions below if any. E → int |…

A: The problem is to eliminate immediate left recursion and left factoring for the production given.

Q: The recursive function fib(n) computes the nth element in the Fibonacci sequence. Implement this…

A: Given The recursive function fib(n) computes the nth element in the Fibonacci sequence. int fib(int…

Q: Instead of reading in an entire list N1,N2,⋯N1​,N2,⋯​ all at once, some algorithms (depending on the…

A: Given  The answer is given below.

Q: Write a recursive function in Python which should take a nested/embedded list and return us the…

A: Program approach: Initialize global variable count. Create function. Declare for loop that runs…

Q: Answer question below: Write Java program. Write an algorithm that takes a sorted list of n…

A: The algorithm is shown below. CountUniqueElements(int arr[],int n){    //Algorithm to count the…

Q: Answer question below: Write Java program. Write an algorithm that takes a sorted list of n…

A: The algorithm is shown below. CountUniqueElementAlgorithm(arr[],n){    //Algorithm to find number of…

Knowledge Booster
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    • SEE MORE QUESTIONS
    Recommended textbooks for you
  • C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
  • C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning