Sara P.

asked • 07/12/20

The following sorting method will rearrange all elements in the array in ascending order. Modify it so that it will sort elements in descending order. Show only the line(s) you would modify.

public static void sort(int[] values) {

int i, j, temp;

for(i=1; i<values.length; i++) {

temp = values[i];

for(j=i-1; j>=0; j--) {

if(values[j]>temp) values[j+1] = values[j];

else break;

}

values[j+1] = temp;

}

}

Patrick B.

you can also print the array backwards: for (int iLoop=array.length-1; iLoop>=0; iLoop--) { System.out.println(array[iLoop]); }
Report

07/12/20

2 Answers By Expert Tutors

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.