File tree 1 file changed +21
-13
lines changed
1 file changed +21
-13
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,12 @@ class BubbleSort
16
16
*/
17
17
public static void main (String [] args )
18
18
{
19
- int array []=new int [6 ];
19
+ int size = 6 ;
20
+ int array []=new int [size ];
21
+ boolean swap ;
22
+ int last = size - 1 ;
20
23
Scanner input =new Scanner (System .in );
24
+
21
25
22
26
//Input
23
27
System .out .println ("Enter any 6 Numbers for Unsorted Array : " );
@@ -27,18 +31,22 @@ public static void main(String[] args)
27
31
}
28
32
29
33
//Sorting
30
- for (int i =0 ; i <5 ; i ++)
31
- {
32
- for (int j =i +1 ; j <6 ; j ++)
33
- {
34
- if (array [j ]>array [i ])
35
- {
36
- int temp =array [j ];
37
- array [j ]=array [i ];
38
- array [i ]=temp ;
39
- }
40
- }
41
- }
34
+ do
35
+ {
36
+ swap = false ;
37
+ for (int count = 0 ; count < last ; count ++)
38
+ {
39
+ if (array [count ] > array [count + 1 ])
40
+ {
41
+ int temp = array [count ];
42
+ array [count ] = array [count + 1 ];
43
+ array [count + 1 ] = temp ;
44
+ swap = true ;
45
+ }
46
+ }
47
+
48
+ last --;
49
+ } while (swap );
42
50
43
51
//Output
44
52
for (int i =0 ; i <6 ; i ++)
You can’t perform that action at this time.
0 commit comments