0

Can anybody do bubble sort in one loop in java

14th Feb 2017, 12:03 PM
Cipher
Cipher - avatar
2 Answers
+ 1
public class BubbleSortInSingleLoop{ public static void main(String[] args) { int list[] = { 45,78,22,96,10,87,68,2 }; for (int i = 1; i < list.length; i++) { if (list[i] < list[i - 1]) { list[i] = list[i] + list[i - 1]; list[i - 1] = list[i] - list[i - 1]; list[i] = list[i] - list[i - 1]; i = 0; } } System.out.print("Sorted listay : "); for (int i = 0; i < list.length; i++) { System.out.print(list[i] + " "); } } }
14th Feb 2017, 12:16 PM
ą¤¦ą„‡ą¤µą„‡ą¤‚ą¤¦ą„ą¤° ą¤®ą¤¹ą¤¾ą¤œą¤Ø (Devender)
ą¤¦ą„‡ą¤µą„‡ą¤‚ą¤¦ą„ą¤° ą¤®ą¤¹ą¤¾ą¤œą¤Ø (Devender) - avatar
0
You can but the solution would be ugly and there is no reason to choose single loop instead of 2
14th Feb 2017, 12:06 PM
Zilvinas Steckevicius
Zilvinas Steckevicius - avatar