How does bubble sort work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How does bubble sort work?

I've tried to use bubble sort, but it shows incorrect result. What I did wrong? https://code.sololearn.com/cjAFK3xV9Vhp/?ref=app

5th Sep 2018, 6:33 PM
Michael
Michael - avatar
2 Answers
+ 1
You are only doing one iteration. As you see, the largest number is at the right position. Apply it n times (where n is the array size) and you will definitely get the correct result. You can also think about how to adjust the indices, so that you don't compare already sorted numbers in the next iterations.
5th Sep 2018, 6:41 PM
Matthias
Matthias - avatar
+ 1
It helped, thx.
5th Sep 2018, 6:45 PM
Michael
Michael - avatar