
David W. answered 03/13/23
Experienced Prof

Jam J.
asked 03/13/23End of Pass 1:
End of pass 2:
David W. answered 03/13/23
Experienced Prof
Juliet C. answered 03/13/23
PhD in Computer Science with 8+ Years of Teaching Experience
The bubble sort algorithm works by comparing adjacent elements, at index p and index p+1 where p starts as the index of the first element, and swapping them if they are not in the correct order and then increasing p by 1 until p is the index of the last element. That is one pass. Note that the same element could be swapped repeatedly during a pass as it "bubbles" through other elements. For example:
9 2 4 7 8
2 9 4 7 8
2 4 9 7 8
2 4 7 9 8
2 4 7 8 9
If any swap occurred during a pass, the algorithm will do another full pass. If no swap occurs during a full pass, the elements are in order and the algorithm stops.
The Wikipedia page has a nice step-by-step example: https://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example
David W.
Correction: "until p is the index of the last element" should be "until p+1 is the index of the last element NOT YET SORTED."03/13/23
Juliet C.
03/17/23
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Juliet C.
03/20/23