A developer implements the following code to sort a singly linked list using bubble sort by swapping data instead of links:
A developer implements the following code to sort a singly linked list using bubble sort by swapping data instead of links:
void sort(struct Node *head) {
struct Node *i, *j;
int temp;
for (i = head; i != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}
}
After sorting, the program prints the list, but the last element sometimes remains unsorted when the list contains duplicate values. What is the most likely conceptual issue?
Show answerHide answer
Correct answer: (C) The outer loop must run until no swaps occur, not just until i == NULL.