0

Why does bubbleSort need two for loops for a single array?

I am trying to understand bubbleSort. After reviewing an answer to this question, it seems that use of a do while loop is best way to do a bubbleSort.

16th Jun 2018, 1:58 AM
LottoMan
2 Answers
+ 6
The inner loop moves the largest to the end of the area being processed. The outer loop start with the full array and shrinks down to 2 elements. Each pass through the outer loop moves the next highest to the last position in the shrinking number of elements. Since you know Java, my version has decent comments and shows what is happening. However, it has optimizations over the regular version. https://code.sololearn.com/c9c17qrFsFZ1
16th Jun 2018, 3:39 AM
John Wells
John Wells - avatar
+ 5
Not sure; In Burey 's visualization I see a while(swapped) wrapping just one 'for' loop: https://code.sololearn.com/WRK8Y6Bkj9eP/?ref=app
16th Jun 2018, 2:10 AM
Kirk Schafer
Kirk Schafer - avatar