April 2017
Beginner
504 pages
14h 11m
English
Now, we can start creating the bubble sort that implements the Sort interface:
package packt.java9.by.example.ch03.bubble; import packt.java9.by.example.ch03.*; import java.util.Comparator; public class BubbleSort implements Sort { @Override public void sort(SortableCollection collection) { int n = collection.size(); while (n > 1) { for (int j = 0; j < n - 1; j++) { if (comparator.compare(collection.get(j), collection.get(j + 1)) > 0) { swapper.swap(j, j + 1); } } n--; } }
Normally, the algorithm to execute needs two operations that we implemented in the code last time specific to a String array: comparing two elements and swapping two elements. As this time the sort implementation itself does not know what ...
Read now
Unlock full access