冒泡排序

坚强是说给别人听的谎言 提交于 2019-11-28 14:42:27

原理

重复比较相邻元素,择大者互换,从而完成排序

开始排序

第一趟

length-1趟

代码实现

  @Test
    public  void Bubblesort() {
        int[] data= {1, 4, 3, 2, 7, 6, 5, 8, 9, 0};
        int position , scan,temp;
        for (position=data.length-1; position >=0 ; position--) {
            for (scan =0;scan<=position-1;scan++) {
                if (data[scan]>(data[scan+1]))
                {
                    temp=data[scan];
                    data[scan]=data[scan+1];
                    data[scan+1]=temp;
                }
            }
        }
        for (int a:data) System.out.println(a);

性能分析

平均时间复杂度: O(n^2)
稳定的

优化方式参考:https://www.jianshu.com/p/f74fdcb2aa0c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!