Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote