Why use boolean for control of bubbleSort? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why use boolean for control of bubbleSort?

I am trying to understand why bubbleSort needs boolean swapped needed to be changed at end of both loops? After commenting out boolean swapped I saw that print statements run without putting inputs in bubbleSort order. I am trying to understand why boolean swapped is needed and why it is set before first loop then changed after first loop.

20th Jun 2018, 5:49 PM
LottoMan
2 Answers
+ 1
Its difficult answer you without code but yes... In "normal" implementation of bubble sort there is a guard for check if in current iteration swap its happened and then break the loop
20th Jun 2018, 11:13 PM
KrOW
KrOW - avatar
0
It appears that the boolean swapped does terminate the loop; there is no reason to continue loop by control i < len-1; if the swapped boolean remains false by the end of any iteration of the bubbleSort for loop.
20th Jun 2018, 7:16 PM
LottoMan