zoukankan      html  css  js  c++  java
  • 交换排序之冒泡排序(java)

    交换排序之冒泡排序(java)

    博客说明

    文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

    说明

    冒泡排序(Bubble Sorting)的基本思想是:通过对待排序序列从前向后(从下标较小的元素开始),依次比较相邻元素的值,若发现逆序则交换,使值较大的元素逐渐从前移向后部,就象水底下的气泡一样逐渐向上冒。

    代码

    package cn.guizimo.sort;
    
    import java.util.Arrays;
    
    public class BubbleSort {
        public static void main(String[] args) {
            int arr[] = {3, 9, -1, 10, -2};
            int temp = 0;
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                System.out.println("第"+(i+1)+"躺排序后的数组");
                System.out.println(Arrays.toString(arr));
            }
        }
    }
    
    
    测试

    image-20200626211951798

    优化

    减少未曾发生交换的次数

    package cn.guizimo.sort;
    
    import java.util.Arrays;
    
    public class BubbleSort {
        public static void main(String[] args) {
            int arr[] = {3, 9, -1, 10, -2};
            System.out.println("排序前");
            System.out.println(Arrays.toString(arr));
            bubbledSort(arr);
            System.out.println("排序后");
            System.out.println(Arrays.toString(arr));
        }
    
        public static void bubbledSort(int[] arr){
            int temp = 0;
            boolean flag = false; //标识,是否发生交换
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        flag = true;
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                if(!flag){ //没有发生交换
                    break;
                }else {
                    //进行下次交换
                    flag = false;
                }
            }
        }
    }
    
    
    测试

    image-20200626212910608

    测试速度

    package cn.guizimo.sort;
    
    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.Date;
    
    public class BubbleSort {
        public static void main(String[] args) {
            int max = 80000;
            int[] arr = new int[max];
            for (int i = 0; i < max; i++) {
                arr[i] = (int)(Math.random() * 8000000);
            }
            long date1 = System.currentTimeMillis();
            bubbledSort(arr);
            long date2 = System.currentTimeMillis();
            System.out.println("冒泡排序"+max+"数组的时间为:"+(date2-date1));
    
        }
    
        public static void bubbledSort(int[] arr){
            int temp = 0;
            boolean flag = false; //标识,是否发生交换
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        flag = true;
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                if(!flag){ //没有发生交换
                    break;
                }else {
                    //进行下次交换
                    flag = false;
                }
            }
        }
    }
    
    

    image-20200626214021562

    运行的时间与自身的电脑有关

    感谢

    尚硅谷

    万能的网络

    以及勤劳的自己
    关注公众号: 归子莫,获取更多的资料,还有更长的学习计划

  • 相关阅读:
    HDU
    Count on a tree
    Codeforces Round #368 (Div. 2) D. Persistent Bookcase
    Codeforces Round #321 (Div. 2) E
    Codeforces Round #220 (Div. 2) D
    树状数组区间加法更新板子
    在 tableview的上面 添加 一个view, 下面 加一个 button,都是 监听 scrollview的滑动而已,
    synchronize,
    菊花,
    4294967295,
  • 原文地址:https://www.cnblogs.com/guizimo/p/13196504.html
Copyright © 2011-2022 走看看