0

Did I code bubble sort correctly? C++

I tried to code my own bubble sort version using C++, but I'm not sure if I did it correctly, I did some test and it works fine, but I don't know how to improve it. I saw some code about bubble sort using nested for loops, but I only used one while, is it ok? The code it's here: https://code.sololearn.com/cqRmiD1oMu0f/#cpp

10th Jan 2020, 4:03 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
2 Respostas
+ 3
No I don't think so. Without running the code I believe what you did should sort the array correctly eventually but it's not a bubblesort. To me it looks more like a cousin of insertion sort. It's slightly inefficient because you jump back to the start of the array more than is necessary, but I mean, not bad for a first try! EDIT: If you remove the i=0 we get into bubblesort territory but then you need that second for loop to reset i to 0 n times.
10th Jan 2020, 7:58 AM
Schindlabua
Schindlabua - avatar
+ 1
Schindlabua you are right, this algorithm is not efficient, i=0 also should be removed, thx!
16th Jan 2020, 10:20 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar