l; while(i

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

For this problem I have a java code:

import java.util.Arrays; import java.util.Scanner; public class MergeSortWeightedSumInversion { public static long merge(int arr[], int l, int mid, int r) { long weight = 0; int n1 = mid-l+1; int n2 = r-mid; int[] a = new int[n1]; int[] b = new int[n2]; for(int i = 0; i < n1; i++) { a[i] = arr[l+i]; } for(int i = 0; i < n2; i++) { b[i] = arr[mid+i+1]; } int i = 0, j = 0, k = l; while(i<n1 && j<n2) { if(a[i] <= b[j]) { arr[k] = a[i]; k++; i++; } else { weight += (a[i] + b[j]); arr[k] = b[j]; k++; j++; } } while(i<n1) { arr[k] = a[i]; k++; i++; } while(j<n2) { arr[k] = b[j]; k++; j++; } return weight; } public static long mergeSort(int arr[], int l, int r) { long weight = 0; if(l < r) { int mid = (l+r)/2; weight += mergeSort(arr, l, mid); weight += mergeSort(arr,mid+1,r); weight += merge(arr,l,mid,r); } return weight; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n; n = scan.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = scan.nextInt(); } System.out.println(mergeSort(arr, 0, n-1)); } } This is wrong because I tried testing it on the array: [7,3,8,1,5] and I got a weight of 26, its only adding up half of the inversions. How would I modify it so that it works? Code in java and time complexity must be O(n log(n)) please.

In the problem of counting inversions, you are given a permutation a₁, a2..., an of numbers
1,2,...,n, and the goal is to count the number of pairs i, j, where i < j and ai > aj. In
this homework problem, you are given a sequence of n numbers b₁,b2,..., bn and your task
is to compute the "weighted count" of inversions defined as follows: An inversion is a pair
of indices i, j where i <j and bi > bj. An i, j inversion has weight b; + b; and the weighted
count for the input sequence is the sum of the weights of all its inversions.
For example, for n = 5 and input sequence 7, 3, 8, 1, 5, we have the following inversions
weights: 7+3= 10, 7+1 = 8, 7+5 = 12, 3+1=4, 8+1=9, and 8+5 = 13. The overall
weighted count is: 10+ 8 + 12 +4+9+13= 56.
Design an O(n log n) algorithm which computes the weighted count of inversions for a
given input sequence.
Transcribed Image Text:In the problem of counting inversions, you are given a permutation a₁, a2..., an of numbers 1,2,...,n, and the goal is to count the number of pairs i, j, where i < j and ai > aj. In this homework problem, you are given a sequence of n numbers b₁,b2,..., bn and your task is to compute the "weighted count" of inversions defined as follows: An inversion is a pair of indices i, j where i <j and bi > bj. An i, j inversion has weight b; + b; and the weighted count for the input sequence is the sum of the weights of all its inversions. For example, for n = 5 and input sequence 7, 3, 8, 1, 5, we have the following inversions weights: 7+3= 10, 7+1 = 8, 7+5 = 12, 3+1=4, 8+1=9, and 8+5 = 13. The overall weighted count is: 10+ 8 + 12 +4+9+13= 56. Design an O(n log n) algorithm which computes the weighted count of inversions for a given input sequence.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education