Let's analyze the provided code and the swap implementation to determine if it correctly implements Bubble Sort on a linked list to sort in ascending order.
Issues and Corrections
-
next initialization and advance: The original code initializes next to head and then immediately increments it in the inner while loop. This means that the first comparison will always compare the head with itself. The comparison is therefore always 0. To fix it, intitialise next to head.getNext()
-
The Swap Logic: The swap method appears to be written for a doubly linked list. When it is swapped in a single-linked list, the previous element to current is not updated to be linked to the newly-swapped element. This means that the list structure will break during the second or later swaps. To fix this the previous node needs to be tracked to swap in a linkedlist. The head must also be updated.
-
Last element not handled: The last pair of elements will not be swapped as the code only swaps when next.getNext() != null.
Corrected Code
public void sort() {
boolean changed = true;
while (changed) {
changed = false;
ISimpleListNode<T> previous = null;
ISimpleListNode<T> current = head;
ISimpleListNode<T> next = (head != null) ? head.getNext() : null; // Corrected next initialization
while (next != null) { // loop through the entire list
if (current.getElement().compareTo(next.getElement()) > 0) {
changed = true;
// Swap the nodes
if (previous == null) { // If swapping head
head = next;
current.setNext(next.getNext());
next.setNext(current);
} else {
previous.setNext(next);
current.setNext(next.getNext());
next.setNext(current);
}
ISimpleListNode<T> temp = current; //for correct movement in list
current = next;
next = temp;
}
previous = current;
current = next;
next = (next != null) ? next.getNext() : null;
}
}
}
Explanation of Changes and Improvements:
next Initialization: ISimpleListNode<T> next = (head != null) ? head.getNext() : null; is initialized so the first comparison is comparing head against its next element, as intended.
- Keep track of previous node: Added a
previous node to properly update the previous node to the swap correctly.
- Head Swap Handling: Added a condition to specifically handle swapping the head of the list if
previous == null.
- Complete traversal: The loop is fixed so the final element is compared also.
swap replacement: Removed the old swap method as it was not suitable for singly linked lists and replaced it with inline node manipulation for swapping.
- Variables to fix loop: The current and next elements need to be updated to move through the list after a swap.
Explanation
-
Outer while (changed) loop: This loop continues as long as there are any swaps made in the inner loop. If no swaps are made in a single pass, it means the list is sorted, and changed remains false, causing the loop to terminate.
-
Inner while (next != null) loop: This loop iterates through the list, comparing adjacent elements.
-
Comparison current.getElement().compareTo(next.getElement()) > 0: This is the core comparison. If the current element is greater than the next element, a swap is needed to maintain ascending order.
-
Swap Logic (Node Manipulation): The if (previous == null) block handles the case where the head of the list needs to be updated. Otherwise, previous.setNext(next) makes the previous node point to the new "first" node of the swapped pair. The rest of the statements perform the standard linked list node rearrangement to swap the positions of current and next.
Why the Original swap Was Incorrect for a Singly Linked List
The original swap logic was trying to modify the next pointers of the nodes involved in the swap. However, in a singly linked list, you cannot directly access the node before current to update its next pointer. Because you don't update the element before the swap you will break your linkedlist. The only way to fix this is to traverse from the head each time, which is not efficient, or to keep track of the previous node.
This corrected version ensures that the list is sorted in ascending order and the structure of the linked list is maintained after each swap.