Advertisement
Shailrshah

Sorting an Array

Aug 24th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3. class Sorting{
  4.     private static String getInput(String prompt){
  5.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  6.         System.out.print(prompt);
  7.         System.out.flush();
  8.         try{
  9.             return stdin.readLine();
  10.         }catch(Exception e){
  11.             return "Error: " + e.getMessage();
  12.         }
  13.     }
  14.     static public void main (String[] args){
  15.         String input = getInput("Enter the size of the array: ");
  16.         int n = Integer.parseInt(input);
  17.         int [] a = new int [n];
  18.         System.out.println("Now enter "+n+" numbers");
  19.         for(int i = 0; i < n; i++){
  20.             input = getInput("Enter element: ");
  21.             a[i] = Integer.parseInt(input);
  22.         }
  23.         Arrays.sort(a);
  24.         for (int number : a)
  25.             System.out.println(+number);
  26.     }
  27. }
  28.  
  29. // Output:-
  30. // C:\Java>java Sorting
  31. // Enter the size of the array: 10
  32. // Now enter 10 numbers
  33. // Enter element: 43
  34. // Enter element: 12
  35. // Enter element: 54
  36. // Enter element: 1
  37. // Enter element: 85
  38. // Enter element: -23
  39. // Enter element: 9
  40. // Enter element: 0
  41. // Enter element: 199
  42. // Enter element: 4000
  43. // -23
  44. // 0
  45. // 1
  46. // 9
  47. // 12
  48. // 43
  49. // 54
  50. // 85
  51. // 199
  52. // 4000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement