Question

Develop and implement a linearithmic algorithm Inversions.java for computing the number of inversions in a given...

Develop and implement a linearithmic algorithm Inversions.java for computing the number of inversions in a given array (the number of exchanges that would be performed by insertion sort for that array—see Section 2.1). This quantity is related to the Kendall tau distance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Code in Java:

import java.util.Scanner;

public class Inversions {
    public static void main(String[] args) {
        int array[] = {4,7,2,9,1};
        //the counter that will count the number of swaps during insertion sort
        int counter=0;
        int n = array.length;
        int temp,i,j;
        for (i = 1; i< n; i++)
        {
            j = i;
            temp = array[i];  
            while (j > 0 && temp < array[j-1])
            {
                array[j] = array[j-1];
                j = j-1;
                //swap found!
                counter++;
            }
            array[j] = temp;          
        }  
        //printing the counter out to the console
        System.out.println(counter);
    }  
}

Console Output:

Add a comment
Know the answer?
Add Answer to:
Develop and implement a linearithmic algorithm Inversions.java for computing the number of inversions in a given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Algorithm Problem - Counting Number of Inversions

    We are given a sequence ofndistinct numbers a1,...,an. We define an inversion to be a pair i<j such that ai>aj. Give an O(nlogn) algorithm to count the number of inversions. (Hint: assume that your algorithmreturns a sorted array of input sequences, as well as the number of inversions. Divide the input sequencesinto two halves, then count inversions and sort each half by doing recursive calls, and find a way to mergethese two sorted halves and to count the number of...

  • 2. Using Python, implement the Divide-and-Conquer algorithm to count the number of inversions between two arrays....

    2. Using Python, implement the Divide-and-Conquer algorithm to count the number of inversions between two arrays. The algorithm is based on Mergesort and counts the inversions while merging the sorted lists.

  • (a) Implement Counting Inversions algorithm in C++. Provide the complete program here which returns the count...

    (a) Implement Counting Inversions algorithm in C++. Provide the complete program here which returns the count and the array. (b) Test your code using this input: a = [2, 6, 1, 5, 4, 3] where array a is the list of your ranking for the given 6 songs.

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...

    ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...

  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them...

    DESCRIPTION Implement a program in C++ that generates a specified number of random integers, records them in three arrays, then sorts the arrays with Insertion Sort, Merge Sort, and Quick Sort, respectively. Augment the three sorting algorithms with counters and report the number of characteristic operations each performs in sorting the (same) random values. INPUT The program reads from the terminal the number of random integers to generate, a seed value for the pseudo-random number generator, and a character that...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT