1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.io.*; public class array_bubble { public static void main()throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int a[], i, j, k, tmp, n; System.out.println("Enter the size of the array:"); n = Integer.parseInt(r.readLine()); a = new int[10]; System.out.println("Enter the values of the array:"); for(i=0;i < n;i++) { a[i] = Integer.parseInt(r.readLine()); } //sorting the array for(i=0;i < n;i++) { for(j=0;j < n-1-i;j++) { if(a[j]>a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } //printing the array System.out.println("The sorted array:"); for(i=0;i < n;i++) { System.out.println(a[i]); } } } |

Rahul Agarwal says

easy to understand and very well written