88 * @see SortAlgorithm
99 */
1010class BubbleSort implements SortAlgorithm {
11+
1112 /**
12- * This method implements the Generic Bubble Sort
13+ * Implements generic bubble sort algorithm.
1314 *
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.
1518 */
1619 @ Override
1720 public <T extends Comparable <T >> T [] sort (T [] array ) {
@@ -30,20 +33,23 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3033 return array ;
3134 }
3235
33- // Driver Program
36+ /** Driver Code */
3437 public static void main (String [] args ) {
3538
36- // Integer Input
3739 Integer [] integers = {4 , 23 , 6 , 78 , 1 , 54 , 231 , 9 , 12 };
3840 BubbleSort bubbleSort = new BubbleSort ();
3941 bubbleSort .sort (integers );
4042
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] */
4347
44- // String Input
4548 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] */
4854 }
4955}
0 commit comments