File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright (C) Deepali Srivastava - All Rights Reserved
3
+ This code is part of DSA course available on CourseGalaxy.com
4
+ */
5
+
6
+ import java .util .Scanner ;
7
+
8
+ public class BubbleSort
9
+ {
10
+ private BubbleSort (){} //this class is not for instantiation
11
+
12
+ public static void sort (int [] a , int n )
13
+ {
14
+ int x ,j ,temp ,swaps ;
15
+
16
+ for (x =n -2 ; x >=0 ; x --)
17
+ {
18
+ swaps =0 ;
19
+ for (j =0 ; j <=x ; j ++)
20
+ {
21
+ if (a [j ] > a [j +1 ])
22
+ {
23
+ temp = a [j ];
24
+ a [j ] = a [j +1 ];
25
+ a [j +1 ] = temp ;
26
+ swaps ++;
27
+ }
28
+ }
29
+ if (swaps ==0 )
30
+ break ;
31
+ }
32
+ }
33
+
34
+ public static void main (String [] args )
35
+ {
36
+ int i ,n ;
37
+ int [] a = new int [20 ];
38
+ Scanner scan = new Scanner (System .in );
39
+
40
+ System .out .print ("Enter the number of elements : " );
41
+ n = scan .nextInt ();
42
+
43
+ for (i =0 ; i <n ; i ++)
44
+ {
45
+ System .out .print ("Enter element " + (i +1 ) + " : " );
46
+ a [i ] = scan .nextInt ();
47
+ }
48
+
49
+ sort (a ,n );
50
+
51
+ System .out .println ("Sorted array is : " );
52
+ for (i =0 ; i <n ; i ++)
53
+ System .out .print (a [i ] + " " );
54
+ System .out .println ();
55
+ scan .close ();
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments