Sotring algorithm | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Sotring algorithm

when I try to code the bubble sorting by myself after the explination I reach that code https://code.sololearn.com/c2Rt89ZLvT0D/?ref=app but I thing it is not a real bubble where is the wrong

18th Aug 2018, 9:05 PM
ABADA S
ABADA S - avatar
6 Answers
+ 1
* Actually you don't need to return the sorted array, because arrays are implemented in objects in java, thus you are passing array arguments by reference not by value(primitive types are passed by value). * in Bubble sort, you don't need to test all array elements every iteration, just first iteration enough. that's why you will see the condition of second for block adjusted to j<=in.length - i
19th Aug 2018, 12:56 PM
Mohammed
+ 3
I but it to block unhelpful iteration can you give me an example
19th Aug 2018, 9:26 AM
ABADA S
ABADA S - avatar
+ 1
It's exactly bubblesort just delete line 21 "if(x==0)break;" that`s wrong.if u exchange 4 and 5 places in array u will see it doesn't work because of that command
18th Aug 2018, 11:07 PM
Hadi Mohseni
Hadi Mohseni - avatar
19th Aug 2018, 6:06 PM
Rajat Tyagi
Rajat Tyagi - avatar
+ 1
check this there it is same as your but with little bit more clear , in second loop you can start were you ended the first one , like j=i
19th Aug 2018, 6:07 PM
Rajat Tyagi
Rajat Tyagi - avatar
0
I adjusted your code as following: https://code.sololearn.com/cR6jdRFoldli/?ref=app
19th Aug 2018, 12:50 PM
Mohammed