Question: Exercise 8 ( Index sorting ) Create method public static int [ ] indexSorting ( int [ ] a ) . It should return a

Exercise 8(Index sorting)
Create method public static int[] indexSorting(int[] a). It should return a table of indices
describing a sort of a, but the table a should not be changed. The following example describes
an application.
[]a={6,10,16,11,7,12,3,9,8,5}; int[] index =
indexSorting(a); // a is unchanged, index is now
{6,9,0,4,8,7,1,3,5,2}.
The result now tells us that the smallest value of a is in position 6, the second smallest value is
in position 9, and so on. The largest value can then be retrieved by looking at the last element
in the index table, so max(a) is therefore a[2], which is 16.
The code
for (int i=0;i a.length; i++) System.out.print(a[index[i]]+
should therefore print out the values of ai in ascending order.
There is no emphasis on efficiency, only that the code works. You can create
auxiliary tables if you wish (and you must create at least one, the table to be returned).
The table a can have equal values, and then the solution will not be unambiguous. If, for
example, the smallest value occurs twice, it does not matter which of them index[0] refers to.
But index[1] must then refer to the second of them. If a is an empty table, the returned table
must also be an empty table.
Exercise 8 ( Index sorting ) Create method public

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!