冒泡排序

public class Demo04 {
public static void main(String[] args) {
int[] arrys = {112,2,55,78,58,96,63,3,21,78};
sort(arrys);
System.out.println(Arrays.toString(arrys));//[2, 3, 21, 55, 58, 63, 78, 78, 96, 112]
}
//冒泡排序
public static int[] sort(int[] a){
//临时变量
int temp = 0;
//外层循环判断要走多少次
for (int i = 0; i < a.length - 1; i++) {
//内层循环,比较两个数,然后进行交互
for (int j = 0; j < a.length - 1 - i; j++) {
if(a[j]>a[j+1]){
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
return a;
}
}
posted @ 2022-04-02 15:58  花田007  阅读(16)  评论(0编辑  收藏  举报