Question

Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....

Java please.

Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list 9 2 5 6 3, the target list would be 2 3 5 6 9. In this case, the 9 is four positions out of place, the 2 is one position out of place, the 5 and 6 are in the correct locations, and the 3 is three positions out of place. Therefore, the distance is 4 + 1 + 0 + 0 + 3 = 8. Keyboard input will consist of a single integer, m, indicating the number of elements in the sequence. Followed on the next line the m integers in the list. Lists will contain at most 20 elements, each of which will be less than 100. For each sequence in the input, display the distance between the given ordering and its sorted counterpart. Refer to the sample output below.

Sample Runs:

Enter the number of elements in the sequence: 5

Enter the 5 integers: 9 2 5 6 3

The distance is: 8

Enter the number of elements in the sequence: 3

Enter the 5 integers: 1 49 99

The distance is: 0

Homework Answers

Answer #1

Code is Given below:

=======================

import java.util.Arrays;
import java.util.Scanner;

public class Distance {
   public static void main(String[] args) {
       //creating scanner object to get input from user
       Scanner scn=new Scanner(System.in);
       System.out.print("Enter the number of elements in the sequence: ");
       int n=scn.nextInt();
       //creating array to hold sequence and sorted numbers
       int []numbers=new int[n];
       int []sorted=new int[n];
       System.out.print("Enter the "+n+" integers: ");
       for(int i=0;i<n;i++) {
           numbers[i]=scn.nextInt();
           sorted[i]=numbers[i];
       }
       //sorting the array
       Arrays.sort(sorted);
       //Initializing distance to 0
       int distance=0;
      
       for(int i=0;i<n;i++) {
           for(int j=0;i<n;j++) {
               if(numbers[i]==sorted[j]) {//checking if numbers are equal
                   distance=distance+Math.abs(j-i);//adding distance
                   break;
               }
              
           }
       }
       //printing result
       System.out.println("The distance is: "+distance);
      
          
      
   }

}

Output:

==========

Code Snapshot:

==================

Know the answer?
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
Similar Questions
You are given a list, L, and another list, P, containing integers sorted in ascending order....
You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L, P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L, P). You may use only the public STL container operations. What is the running time of your procedure?
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci...
In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn with seed values F1 = 1 F2 = 1 For more information on...
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
A sequence is a list of numbers that are calculated based on a certain rule. For...
A sequence is a list of numbers that are calculated based on a certain rule. For instance, the progression described by the rule An = 2 ∗ n results in the numbers: 0 2 4 6 8 10 ··· 2 ∗ n. The sum of this sequence can be calculated as Sn = 0+2+4+6+8+10+···+2 ∗ n. Write a function that takes as input the number n and calculates the sum of the sequence up to the nth term (inclusive) for...
Must use loops. Please do not use sequence of if statements. I need it in C++....
Must use loops. Please do not use sequence of if statements. I need it in C++. Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. for input of "xxcaazz", "xxbaaz" → 3 for input of "abc", "abc" → 2 for input of "abc", "axc" → 0
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels...
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels are required for a list of 64 elements compared to a list of 8 elements? a. 3 b. 9 c. 8 d. 6 2. Which function best represents the number of operations in the worst-case for the following code fragment? for (i = 0; i < N; ++i) {    if (numbers[i] % 2 == 1)       factor = 2.5 } a. f(N) = 6N2 b....
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as...
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as argument and determine the elements and indices of those elements that preceed a negative number in the given list. The returned information should be in the form of a pair of lists: ((values) . (indices)). E.g. (preceeding '(1 -2 3 4 -5 6 -7 -8)) → ((1 4 6 -7).(0 3 5 6)) ;note: drracket will display this as ((1 4 6 -7) 0...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...