fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static void printArr(int arr[]) {
  11. for(int i = 0; i < arr.length; i++) {
  12. System.out.print(arr[i]+" ");
  13. }
  14.  
  15. System.out.println();
  16. }
  17.  
  18.  
  19. private static void bubbleSort(int arr[]) {
  20. int size = arr.length;
  21.  
  22. for(int i = 0; i < size -1; i++) {
  23.  
  24. for(int j = 0; j < size-1-i; j++) {
  25. if(arr[j+1] < arr[j]) {
  26. int tmp = arr[j];
  27. arr[j] = arr[j+1];
  28. arr[j+1] = tmp;
  29. }
  30. }
  31.  
  32. printArr(arr);
  33.  
  34. }
  35.  
  36. }
  37.  
  38. public static void main (String[] args) throws java.lang.Exception
  39. {
  40. Scanner sc = new Scanner(System.in);
  41. int n = sc.nextInt();
  42.  
  43. int[] arr = new int[n];
  44.  
  45. for(int i = 0; i < arr.length; i++) {
  46. arr[i] = sc.nextInt();
  47. }
  48. sc.close();
  49.  
  50.  
  51.  
  52.  
  53. bubbleSort(arr);
  54.  
  55. printArr(arr);
  56. }
  57. }
Success #stdin #stdout 0.2s 51784KB
stdin
5
5 4 3 2 1
stdout
4 3 2 1 5 
3 2 1 4 5 
2 1 3 4 5 
1 2 3 4 5 
1 2 3 4 5