Back to Problem Solutions forum
import java.util.*; public class BubbleSort{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int[] m; m = new int[in.nextInt()]; for(int i = 0; i < m.length; i++){ m[i] = in.nextInt();} int temp = 0; int sch = 0; int sch1 = 0; for (int k = 0; k < m.length; k++) { sch1++; for (int j = 1; j < (m.length - k); j++) { if (m[j - 1] > m[j]) { temp = m[j - 1]; m[j - 1] = m[j]; m[j] = temp; sch++; } } } System.out.print((sch1) +" "+ sch); } }
Expected answer was: 17 104 Your answer was: 23 104
I can not understand what the problem is and how to make the right decision
import java.util.*; public class BubbleSort{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int[] m; m = new int[in.nextInt()]; for(int i = 0; i < m.length; i++){ m[i] = in.nextInt();} int temp = 0; int sch = 0; int sch1 = 0; for (int k = 0; k < m.length; k++) { sch1++; for (int j = 1; j < (m.length - k); j++) { if (m[j - 1] > m[j]) { temp = m[j - 1]; m[j - 1] = m[j]; m[j] = temp; sch++; } } } System.out.print((sch1) +" "+ sch); } }
Read carefully :)
To test it we will check the amount of passes and amount of swaps made before the given array becomes ordered.
...
It is obvious, that if the pass do not perform any swaps, the array is already sorted and future passes could not change anything.
Also, please read Help on Formatting, especially the part about code blocks. Thanks.