0
Can anybody do bubble sort in one loop in java
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] + " ");
}
}
}
0
You can but the solution would be ugly and there is no reason to choose single loop instead of 2