冒泡排序算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class maopao {
    public static void main(String[] args) {
        int a[] = {77,66,9,100,1};
        System.out.print("排序之前的数组:");
        for (int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
        //第一层循环是冒泡的总次数---为(n-1)
        //第二层循环是单次循环的次数---为(n-i)
        //if判断 然后交换前后数
        for (int i=1;i<=a.length-1;i++) {
            for (int j = 1; j <= a.length - i; j++) {
                if (a[j - 1] > a[j]) {
                    int temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.println();
        System.out.print("排序之后的数组:");
        for (int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
 
 
    }
}

  

posted @   ttsx123  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示