public static void main(String[] args) {
int[] x = {45,66,11,34,56,76,23,45,76,345,77};
bubble(x);
System.out.println("min is "+x[x.length-1]);
System.out.println("max is "+x[0]);
}
//-----------------------冒泡排序
public static void bubble(int[]a){
for(int x = 1;x <a.length;x++){
for(int y =0;y < x;y++){
if(a[x]>=a[y]){
a[x] = a[x]^a[y];
a[y] = a[x]^a[y];
a[x] = a[x]^a[y];
}
}
}
}
简单思路可以先排序后输出。得到的结果是
min is 11
max is 345
实际上可以只遍历1次也就可以得到,都不用整体排序
public static void main(String[] args) {
int[] x = {45,66,11,34,56,76,23,45,76,345,77};
int min = x[0];
int max =x[0];
for(int i =0;i<x.length;i++){
if(min>x[i]){
min = x[i];
}
if(max<x[i]){
max =x[i];
}
}
System.out.println(min+":"+max);
}
这样输出11:345
温馨提示:答案为网友推荐,仅供参考