Question
Write a program called Inversions that contains a method that takes as a parameter an int array and returns the number of inversions in it.
Write a program called Inversions that contains a method that takes as a parameter an int array and returns the number of inversions in it. Write a main method that fills an array of length 1000 with randomly generated values, prints the number inversions in it, sorts the array using any sort of your choice, and again prints the number of inversions.
Here is the algorithm for counting inversions:
inversions(a) count = 0 for i = 0 to a.length-1 for j = i+1 to a.length-1 if a[i] > a[j] then increment count return count
An inversion in an int array is a pair of elements a[i] and a[j] where ia[j]. In other words, it's a pair of elements in the wrong order. The idea of a sorting algorithm is to reduce the number of inversions to zero. Using the pseudocode above, write the program in Java, please.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started