File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ class BubbleSorting {
4
+ public static void main (String args [])
5
+ {
6
+ Scanner scan = new Scanner (System .in );
7
+ // Taking the length of array as input from user
8
+ System .out .println ("Enter the size of the array:" );
9
+ int len = scan .nextInt ();
10
+ // Creating array of the entered length
11
+ int arr [] = new int [len ];
12
+ // Taking array contents as input from user
13
+ System .out .println ("Enter the array elements:" );
14
+ for (int i = 0 ; i <= arr .length -1 ; i ++) {
15
+ System .out .println ("Enter an element:" );
16
+ arr [i ] = scan .nextInt ();
17
+ }
18
+ // Printing the array contents before sorting
19
+ System .out .println ("Array contents before sorting:" );
20
+ for (int i = 0 ; i <= arr .length -1 ; i ++) {
21
+ System .out .print (arr [i ] + " " );
22
+ }
23
+ // Sorting the array contents in ascending order using bubble sort
24
+ int help ;
25
+ for (int i = 0 ; i <= arr .length -2 ; i ++) {
26
+ for (int j = 0 ; j <= arr .length -2 -i ; j ++) {
27
+ if (arr [j ] > arr [j +1 ]) {
28
+ help = arr [j ];
29
+ arr [j ] = arr [j +1 ];
30
+ arr [j +1 ] = help ;
31
+ }
32
+ }
33
+ }
34
+ // Printing the sorted array contents
35
+ System .out .println ();
36
+ System .out .println ("Sorted array contents are:" );
37
+ for (int i = 0 ; i <= arr .length -1 ; i ++) {
38
+ System .out .print (arr [i ] + " " );
39
+ }
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments