How Array are Sort In Java? Ascending or Descending

Here we will learn How Array are Sort In Java? Ascending or Descending As there are a number of algorithms to sort an array but here we will use an inbuild function of
Array. sort(arr, Collections.reverseOrder()) which will sort the array arr and return the sorted array in descending order.

Array are Sort In Java

Array Sorting

Array sorting means arranging the elements of arrays in a given order whether ascending or descending so we will sort the array using an inbuild function here. For using this inbuild function need to import a library of java that is Collections so that we can the result sorted.

Although there are a number of algorithms to sorting an array

The List of Sorting algorithms is.

  1. Selection Sort
  2. Bubble Sort
  3. Insertion Sort
  4. Heap Sort
  5. Quick Sort
  6. Merge Sort
  7. Bucket Sort
  8. Radix Sort
  9. Count Sort
  10. shell Sort
  11. Tim Sort
  12. Tree Sort
  13. Cube Sort

Which can be used for sorting the array. Each Sorting algorithm have a specific approach to sort and have different complexities But here we can directly sort using an in build function

Although This inbuils Function follow certain algorithm to sort the array.

Now, time for an example so that we can understand in a better way how to sort the array using the inbuild function.

import java.util.Arrays;
import java.util.Collections;

class ArraySort {
    public static void main(String[] args) {
        Integer[] arr = { 5, 2, 1, 8, 10 };
        Arrays.sort(arr, Collections.reverseOrder());
        
        for (int values : arr) {
            System.out.print(values + ", ");
            // 10, 8, 5, 2, 1,
        }
    }
}

Here we can see how an array arr with elements { 5, 2, 1, 8, 10 } get sorted using Arrays.sort(arr, Collections. reverseOrder());  and we get the desired output as 10, 8, 5, 2, 1, which we have printed using a for loop so the complexity of printing an array here is linear.

So we can draw a conclusion that printing a one dimensional array has the complexity of linear printing.

Learn more about How Array is Sort In Java? Ascending or Descending At: Array Sorting

Also visit Java Tutorialsfor learn and solve your problems faced during learning java also for Python you can visit Python’s list: What is the difference Between methods Append and Extend? And related to that topics can be seen there.

Leave a Comment

%d bloggers like this: