JAVA之求一维数组中最大值与最小值(利用冒泡排序)

时间:2022-03-09 15:14:18
package test;

public class demo {
static void maopao(int num[])
{
int i,j,temp;
for(i=0;i<num.length-1;i++)
{
for(j=0;j<num.length-1-i;j++)
{
if(num[j]>num[j+1])//交换元素
{
temp=num[j+1];
num[j+1]=num[j];
num[j]=temp;
}
}
}
}
public static void main(String[] args) {
int i,j;
int num[]={2,51,22,14,36,45,88,10,12,31};
maopao(num);
System.out.println("最大数为:");
System.out.println(num[9]);
System.out.println("最小数为:");
System.out.println(num[0]);
System.out.println("差值为:");
j=num[9]-num[0];
System.out.println(j);

}

}
//本代码仅供参考,不保证正确性哦~