Computer Science, asked by abcd65807, 2 months ago

Write a program in java to accept an array of 15 elements of float datatype and sort in descending order using bubble Sort technique.​

Answers

Answered by shakshiroy
0

Answer:

you can sort an object array in decreasing or reverse order, just provide a Comparator with the opposite order. You can even use Collections.reverseOrder if you want to sort an array in the decreasing order, which returns a reverse Comparator to sort objects in the order opposite of their natural ordering defined by the compareTo method.

Unfortunately, for a primitive array, there is no direct way to sort in descending order. The Arrays.sort method which is used to sort a primitive array in Java doesn't accept a boolean to sort the primitive array in reverse order.

You might have seen the error "no suitable method found for sorti ntcomparators object which occurs when programmers try to call the Arrays.sort method by passing reverse Comparator defined bythe Collection.reverseOrder method.

That will work fine with an Integer array but will not work with an int array. The only way to sort a primitive array in descending order is first to sort the array in ascending order and then reverse the array in place as shown here. This is also true for two-dimensional primitive arrays.

Similar questions