No nonsense, just on the code.
1. Get the maximum number of 5 numbers:
Int[] score = {66,77,88,65,88};
int max = score[0];
for (int i=0;i<score.length-1;i++) {
if (Max<score[i+1]) {
max = score[i+1];
}
}
As you can see, the total number of calculations is
Number of Calculations 2 3 4 5 ... n
Number of Calculations 1 2 3 4 ... n-1
The minimum number of times.
2. Arrange 6 numbers from small to large: (bubbling)
Int[] A = {55, 45, 23, 444, 313, 112};
int C;
int count = 0;
for (int i = 1; i < a.length; i++) {
for (int j = 0; J < A.length-i; J + +) {
if (A[j] > a[j + 1]) {
c = a[j + 1];
A[j + 1] = A[j];
A[J] = c;
}
count++;
}
}
for (int i = 0; i < a.length; i++) {
System.out.println (A[i] + "= =" + count+ "= = =" +a.length);
}
Output:
---------------------
23==15===6
45==15===6
55==15===6
112==15===6
313==15===6
444==15===6
---------------------
Bubble Analysis:
c = a[j + 1];
A[j + 1] = A[j];
A[J] = c;
The meaning of the existence of variable C is to store a[j+1] and put a[j] after the substitution.
(Don't be offended by drawing)
The number of times the exchange of Bubbles is, (5,4,3,2,1)
The purpose of each completion of the internal for loop is to put the maximum number at the end and remove it at the next loop, so the number of times is reduced by one.
Questions about the number of sort calculations in an array