Complete the while loop compare cursor and the next element
Complete the while loop, compare cursor and the next element and if they are out of order then swap.
private void bubbleSort(Comparator<Task> comparator) {
boolean swapped = false;
Node cursor = _head;
while (cursor != null && cursor.after != null) {
//TODO
// compare cursor and the next element
// if they are out of order then swap
cursor = cursor.after;
}
if (swapped) bubbleSort(_comparator);
}
Solution
private void bubbleSort(Comparator<Task> comparator) {
boolean swapped = false;
Node cursor = _head;
while (cursor != null && cursor.after != null) {
swapped = False;
if (cursor > cursor.after)
{
exchanges = True;
temp = cursor ;
cursor.after: = temp;
cursor = cursor.after;
}
}
if (swapped) bubbleSort(_comparator);
}
