8
8
* @see SortAlgorithm
9
9
*/
10
10
class BubbleSort implements SortAlgorithm {
11
+
11
12
/**
12
- * This method implements the Generic Bubble Sort
13
+ * Implements generic bubble sort algorithm.
13
14
*
14
- * @param array The array to be sorted Sorts the array in ascending order
15
+ * @param array the array to be sorted.
16
+ * @param <T> the type of elements in the array.
17
+ * @return the sorted array.
15
18
*/
16
19
@ Override
17
20
public <T extends Comparable <T >> T [] sort (T [] array ) {
@@ -30,20 +33,23 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
30
33
return array ;
31
34
}
32
35
33
- // Driver Program
36
+ /** Driver Code */
34
37
public static void main (String [] args ) {
35
38
36
- // Integer Input
37
39
Integer [] integers = {4 , 23 , 6 , 78 , 1 , 54 , 231 , 9 , 12 };
38
40
BubbleSort bubbleSort = new BubbleSort ();
39
41
bubbleSort .sort (integers );
40
42
41
- // Output => 1, 4, 6, 9, 12, 23, 54, 78, 231
42
- print (integers );
43
+ for (int i = 0 ; i < integers .length - 1 ; ++i ) {
44
+ assert integers [i ] <= integers [i + 1 ];
45
+ }
46
+ print (integers ); /* output: [1, 4, 6, 9, 12, 23, 54, 78, 231] */
43
47
44
- // String Input
45
48
String [] strings = {"c" , "a" , "e" , "b" , "d" };
46
- // Output => a, b, c, d, e
47
- print (bubbleSort .sort (strings ));
49
+ bubbleSort .sort (strings );
50
+ for (int i = 0 ; i < strings .length - 1 ; i ++) {
51
+ assert strings [i ].compareTo (strings [i + 1 ]) <= 0 ;
52
+ }
53
+ print (bubbleSort .sort (strings )); /* output: [a, b, c, d, e] */
48
54
}
49
55
}
0 commit comments