简单冒泡排序:
package SomeClass;
import java.util.Arrays;
public class TestMaopao {
public static void main(String[] args) {
int[] a = {124,46,4,64,12,3,89,1568,9542,1556,6,9,5};
Maopaosort(a);
System.out.print(Arrays.toString(a));
}
public static void Maopaosort(int[] b){
int temp;
for (int i = 0; i <b.length ; i++) {
// boolean flag = true;
for (int j = 0; j <b.length-1-i ; j++) {
if (b[j]>b[j+1]) {
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
// flag = false;
}
}
// if (flag) { break; }
}
}
}
注释部分为其优化
运行结果:
[3, 4, 5, 6, 9, 12, 46, 64, 89, 124, 1556, 1568, 9542]
Process finished with exit code 0