Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 Programming Assignment In a sequence S = [S1,S2, ..., Sn] of n integers, an inversion is a pair of elements si and s; where

image text in transcribed
1 Programming Assignment In a sequence S = [S1,S2, ..., Sn] of n integers, an inversion is a pair of elements si and s; where i S;. For example, in the sequence S = 2,1,5,3,4 the pairs (2,1), (5,3) and (5,4) are inversions. The programming assignment is to implement an algorithm which counts the number of inversions in an input sequence: Input: An array A of n integers in the range 1 to n. Output: An integer, corresponding to the number of inversions in A. An array with n elements may have as many as n(n-1) 2 inversions. When the number of inversions, k, may be any value between 0 and n(n-1) the best algorithm for counting inversions has running time O(n log n). There also exists a O(n + k) algorithm for counting inversions, which is 0(na) when k 0(na). 2 In this assignment, we will assume that the number of inversions is at most n (that is, k sn). When the O(n + k) algorithm is used with such values of k, its running time is 0(n + n) O(n). For full marks, your implementation must run in O(n) time when k sn. Your task is to write a java program, stord; in a file named Count Inversions.java that contains a function CountInversions, which takes an integer array A as its only argument, and returns an integer value. The main function in your code should help you test your implementation by getting test data or reading it from a file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

What did they do? What did they say?

Answered: 1 week ago

Question

2. Do you find change a. invigorating? b. stressful? _______

Answered: 1 week ago