冒泡排序

 1 package 基础语法练习;
 2 
 3 public class 冒泡排序 {
 4     public static void main(String[] args) {
 5 
 6         // 定义一个不规则的数组
 7         int[] array = {10, 2, 1, 9, 4, 88, 77, 81};
 8         // 当前数组长度
 9         int len = array.length;
10 
11         int temp = 0;
12 
13         // 进行冒泡排序+
14         for (int i = 0; i < len - 1; i++) {
15             for (int j = 0; j < len - 1; j++) {
16                 if (array[j + 1] < array[j]) {
17                     temp = array[j + 1];
18                     array[j + 1] = array[j];
19                     array[j] = temp;
20                 }
21             }
22         }
23 
24         // 输出打印数组
25         for (int i = 0; i < len; i++) {
26             System.out.print(array[i] + "\t");
27         }
28 
29     }
30 }

 

输出结果

 

 

posted @ 2022-01-15 17:20  Chris丶Woo  阅读(6)  评论(0编辑  收藏  举报