
public class Bubble {
public static void sort(Comparable[] a){
for(int i=a.length-1;i>0;i--){
for(int j=0;j0;
}
private static void exch(Comparable[] a,int i,int j){
Comparable temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
//测试类
import java.util.Arrays;
public class BubbleTest {
public static void main(String[] args) {
Integer[] arr={4,5,2,6,1,3};
Bubble.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
结果:
D:javajdkjdk-14.0.2binjava.exe "-javaagent:D:ideaIC-2021.2.2IDEAIntelliJ IDEA 2020.1.1libidea_rt.jar=50316:D:ideaIC-2021.2.2IDEAIntelliJ IDEA 2020.1.1bin" -Dfile.encoding=UTF-8 -classpath E:ideaProjectitcastoutproductiondata_structure BubbleTest
[1, 2, 3, 4, 5, 6]
Process finished with exit code 0
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)