Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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

Mobile Usability

Authors: Jakob Nielsen, Raluca Budiu

1st Edition

0133122131, 9780133122138

More Books

Students also viewed these Programming questions