| 1 | public class Main { |
| 2 | static void bubbleSort(int[] a){ |
| 3 | int n = a.length; |
| 4 | // n-1 iterations/passes |
| 5 | for(int i = 0; i < n-1; i++){ |
| 6 | boolean flag = false; // has any swapping happened |
| 7 | for(int j = 0; j < n-i-1; j++){ |
| 8 | /* |
| 9 | last i elements are already at correct sorted positions, |
| 10 | so no need to check them |
| 11 | */ |
| 12 | if(a[j] > a[j+1]){ |
| 13 | // swap - a[j], a[j+1] |
| 14 | int temp = a[j]; |
| 15 | a[j] = a[j+1]; |
| 16 | a[j+1] = temp; |
| 17 | flag = true; // some swap has happened |
| 18 | } |
| 19 | } |
| 20 | if(!flag){ // have any swaps happened? |
| 21 | return; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public static void main(String[] args) { |
| 27 | int[] a = {5, 4, 1, 2, 3, 6, 0}; |
| 28 | bubbleSort(a); |
| 29 | for (int i : a) { |
| 30 | System.out.print(i + " "); |
| 31 | } |
| 32 | } |
| 33 | } |
nothing calls this directly
no outgoing calls
no test coverage detected