Java对一维数组排序:冒泡,选择,插入,比较效率

对一维数组排序:冒泡,选择,插入,比较效率

        long start=System.currentTimeMillis();

        //程序代码
        long end=System.currentTimeMillis();

        System.out.println("消耗时间:"+(end-start)+"毫秒");

循环遍历数组的数据,提高代码的重用性

/**
     * @description 数组没有排序之前循环遍历输出数据
     * @param a 数组
     */
    public static void AfterSort(int[] a){
        System.out.print("未排序的数组:");
        for(int k=0;k<a.length;k++)System.out.print(a[k]+",");
        System.out.println();
    }
    /**
     * @description 数组在进行排序之后循环遍历出数据
     * @param 采用的排序方法   Ex:选择排序
     * @param 数组
     */
    public static void BeforeSort(String str,int[] a){
        System.out.print(str+"后的数组:");
        for(int i=0;i<a.length;i++)System.out.print(a[i]+",");
    }

一、选择排序:让每个元素和后面的元素逐一比较如果大就交换。

public static void SelectionSort(){
        int[] a = new int[]{3,8,2,1,9,7,6,4,0,5};
        AfterSort(a);//调用方法
        for(int i=0;i<a.length-1;i++){  //让每个元素
            for(int j=i+1;j<a.length;j++){  //后边的每个元素
                //比较
                if(a[i]>a[j]){
                    int t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }
            }
        }
        BeforeSort("选择排序",a);
    }

这里写图片描述

二、冒泡排序:比较相邻的元素。如果第一个比第二个大,就进行交换

public static void BubbleSort(){
        int[] a = new int[]{3,8,2,1,9,7,6,4,0,5};
        AfterSort(a);//调用方法
        for(int k=1;k<a.length-1;k++){//次数
            for(int i=0;i<a.length-k;i++){//遍历每一次的元素
                //比较
                if(a[i]>a[i+1]){
                    int t=a[i];
                    a[i]=a[i+1];
                    a[i+1]=t;
                }
            }
        }
        BeforeSort("冒泡排序",a);
    }

这里写图片描述
三、插入排序:将一个数插入到一个有序的序列中并保持有序

public static void InsertSort(){
        int[] a = new int[]{3,8,2,1,9,7,6,4,0,5};
        AfterSort(a);//调用方法
        for(int i=1;i<a.length;i++){//从第二个数开始
            //插入到前面已经排序的元素
            for(int j=i-1;j>=0;j--){
                if(a[j+1]<a[j]){
                    int t=a[j];
                    a[j]=a[j+1];
                    a[j+1]=t;
                }else break;
            }
        }
        BeforeSort("插入排序",a);
    }

这里写图片描述

最后就是比较各排序算法的效率

public static void getSource(){
        Random random = new Random();//创建随机数对象
        int count=100000;//声明数组的长度
        int[] a= new int[count];
        int[] b= new int[count];
        int[] c= new int[count];

        for(int i=0;i<count;i++){
            int value = random.nextInt(count);//生成0-100000的随机数
            a[i]=b[i]=c[i]=value;//赋值
        }
        long start=System.currentTimeMillis();
        SelectionSort(a);
        long end=System.currentTimeMillis();
        System.out.println("选择排序消耗时间:"+(end-start)+"毫秒");

        start=System.currentTimeMillis();
        BubbleSort(b);
        end=System.currentTimeMillis();
        System.out.println("冒泡排序消耗时间:"+(end-start)+"毫秒");

        start=System.currentTimeMillis();
        InsertSort(c);
        end=System.currentTimeMillis();
        System.out.println("插入排序消耗时间:"+(end-start)+"毫秒");
    }

这里写图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值