In: Computer Science
Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
/*If you have any query do comment in the comment section else like the solution*/
import java.util.*;
public class SortArrays {
public static void sortArrayInAscendingOrder(Integer arr[]) {
Arrays.sort(arr);
}
public static void sortArrayInDescendingOrder(Integer arr[]) {
Arrays.sort(arr, Collections.reverseOrder());
}
public static void printArray(Integer arr[], int n) {
for(int i=0;i<n;i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter 10 integers : ");
Integer arr[] = new Integer[10];
for(int i=0;i<10;i++) {
arr[i] = sc.nextInt();
}
sortArrayInAscendingOrder(arr);
System.out.println("Array in ascending order : ");
printArray(arr, 10);
sortArrayInDescendingOrder(arr);
System.out.println("Array in descending order : ");
printArray(arr, 10);
}
}
Output: