Complete the while loop, compare cursor and the next element and if they are out
ID: 3921027 • Letter: C
Question
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);
}
Explanation / Answer
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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.