Array sorting using sort functing in JAVA programming


  1.  
  2. //array sorting using sort function ..............
  3.  
  4. import java.util.Arrays;
  5.  
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class ArraySort {
  10.  
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Scanner myScanner=new Scanner(System.in);
  15.  
  16. System.out.print("Enter the number of elements :");
  17.  
  18. int n=myScanner.nextInt();
  19.  
  20.  
  21.  
  22. int[] arr=new int[n];
  23.  
  24. System.out.print("Enter "+n+" numbers : ");
  25.  
  26. for (int i = 0; i < n; i++) {
  27.  
  28. arr[i]=myScanner.nextInt();
  29.  
  30. }
  31.  
  32. Arrays.sort (arr);
  33.  
  34. System.out.print("Sorted list are : ");
  35.  
  36.  
  37.  
  38. for (int number : arr) {
  39.  
  40. System.out.print(number+"\t");
  41.  
  42.  
  43.  
  44. }
  45.  
  46. }
  47.  
  48.  
  49. }
  50.  
  51. //Enter the number of elements :5
  52.  
  53. //Enter 5 numbers : 7 3 2 34 3
  54.  
  55. //Sorted list are : 2 3 3 7 34
  56.  
  57.  
  58.  

No comments:

Post a Comment