java中最简单的方法冒泡排序?

2021-11-04 教育 205阅读
publicclassBubble{

//冒泡排序函数1
publicstaticvoidbubbleSort1(Comparable[]data){

intposition,scan;
Comparabletemp;
for(position=data.length-1;position>=0;position--){
for(scan=0;scan<=position-1;scan++){
if(data[scan].compareTo(data[scan+1])<0){
temp=data[scan];
data[scan]=data[scan+1];
data[scan+1]=temp;
}
}
}
}
//冒泡排序函数2
publicstaticint[]bubbleSort2(int[]m){

intintLenth=m.length;

for(inti=0;i
for(intj=0;j{
inta=m[j];
intb=m[j+1];
if(a{
m[j]=b;
m[j+1]=a;
}
}
}
returnm;
}

publicstaticvoidmain(String[]args){

//冒泡排序1
Comparable[]c={4,9,23,1,45,27,5,2};
bubbleSort1(c);
for(inti=0;iSystem.out.println("冒泡排序1:"+c[i]);

System.out.println("*******************");

//冒泡排序2
int[]b={4,9,23,1,45,27,5,2};
int[]e=bubbleSort2(b);
for(intj=0;jSystem.out.println("冒泡排序2:"+e[j]);
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com