向高性能前进之 冒泡排序法优化

目录

前言:

向高性能前进之 冒泡排序法​编辑

优化效果

冒泡法优化原理 --- 如下图

原始冒泡法 --- 代码

改进后冒泡法 --- 代码

测试源代码


前言:

冒泡排序是一种简单但不太有效的排序算法,它的时间复杂度为 O(n^2),在处理大规模数据时会变得非常慢。然而,通过一些优化措施,我们可以将冒泡排序的性能提高到接近 O(n log n) 的水平。

向高性能前进之 冒泡排序法😀

性能调优可以分为:硬件调优、网络调优、架构调优、组件配置调优、业务调优、数据库调优、代码调优等,此篇就是简单的代码调优实践

引用:马士兵老师的课程,以日期(年,月,日)数组为例,进行递增排序

优化效果

  • 日期数组元素为 1000 个时,原始冒泡法耗时 9 毫秒,改进后冒泡法耗时 4 毫秒,性能优化 55.55%;
  • 日期数组元素为 5000 个时,原始冒泡法耗时 162 毫秒,改进后冒泡法耗时 76 毫秒,性能优化 53.08%;
  • 日期数组元素为 1 万个时,原始冒泡法耗时 673 毫秒,改进后冒泡法耗时 299 毫秒,性能优化 55.57%;
  • 日期数组元素为 2 万个时,原始冒泡法耗时 2966 毫秒,改进后冒泡法耗时 1341 毫秒,性能优化 54.78%;
  • 日期数组元素为 3 万个时,原始冒泡法耗时 6928 毫秒,改进后冒泡法耗时 3157 毫秒,性能优化 54.43%;

综上所述:通过对冒泡法的改进后,当日期数组元素>=1000 时,性能优化了至少 50%。

冒泡法优化原理 --- 如下图

冒泡法优化原理图

原始冒泡法 --- 代码

public static void BubbleSort(DateArray[] d) {  //递增排序,普通冒泡法
    DateArray demo = new DateArray(2016,12,12);
    for(int i=0; i<d.length; i++) {
        for(int j=i+1; j<d.length; j++) {
            if(d[i].compare(d[j])>0) {
                demo = d[i];
                d[i] = d[j];
                d[j] = demo;
            }
        }
    }
}

改进后冒泡法 --- 代码

public static void BubbleSortGood(DateArray[] d) { //递增排序,改进冒泡法
    int z=0;
    DateArray demo = new DateArray(2016,12,12); 
    for(int i=0; i<d.length; i++) {
        z = i;
        for(int j=z+1; j<d.length; j++) {
            if(j < d.length) {
                if(d[z].compare(d[j])>0) {
                    z = j;
                }
            }else {
                demo = d[i];
                d[i] = d[j];
                d[j] = demo;
            }
        }
    }
}

测试源代码

public class TestDataSort {
    public static void main(String[] args) {
        long starttime_BubbleSort;  //原始冒泡法开始时间 变量定义
        long endtime_BubbleSort;    //原始冒泡法结束时间 变量定义
        long starttime_BubbleSortGood;  //改进冒泡法开始时间 变量定义
        long endtime_BubbleSortGood;    //改进冒泡法结束时间 变量定义

        //日期数据的定义及初始化
        DateArray[] d = new DateArray[1000];
        for(int i=0; i<d.length; i++) {
            d[i] = new DateArray((int)(2020+Math.random()*(2020-1990+1)),(int)(12+Math.random()*(12-1+1)),(int)(30+Math.random()*(30-1+1)));
        }

        //原始冒泡法计时及耗时输出
        starttime_BubbleSort = System.currentTimeMillis();
        BubbleSort(d);
        endtime_BubbleSort = System.currentTimeMillis();
        System.out.println("原始冒泡法耗时"+(long)(endtime_BubbleSort-starttime_BubbleSort)+"毫秒");

        System.out.println("----------------------------------------------");

        //改进冒泡法计时及耗时输出
        starttime_BubbleSortGood = System.currentTimeMillis();
        BubbleSortGood(d);
        endtime_BubbleSortGood = System.currentTimeMillis();        
        System.out.println("改进后冒泡法耗时"+(long)(endtime_BubbleSortGood-starttime_BubbleSortGood)+"毫秒");
    }

//递增排序,普通冒泡法
    public static void BubbleSort(DateArray[] d) {  
        DateArray demo = new DateArray(2016,12,12);
        for(int i=0; i<d.length; i++) {
            for(int j=i+1; j<d.length; j++) {
                if(d[i].compare(d[j])>0) {
                    demo = d[i];
                    d[i] = d[j];
                    d[j] = demo;
                }
            }
        }
    }

//递增排序,改进冒泡法
    public static void BubbleSortGood(DateArray[] d) { 
        int z=0;
        DateArray demo = new DateArray(2016,12,12); 
        for(int i=0; i<d.length; i++) {
            z = i;
            for(int j=z+1; j<d.length; j++) {
                if(j < d.length) {
                    if(d[z].compare(d[j])>0) {
                        z = j;
                    }
                }else {
                    demo = d[i];
                    d[i] = d[j];
                    d[j] = demo;
                }
            }
        }
    }

}

//日期数组类定义
class DateArray {
    int year;
    int month;
    int day;
    public DateArray(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public int compare(DateArray d) {
        return year > d.year ? 1
                : year < d.year ? -1
                : month > d.month ? 1
                : month < d.month ? -1
                : day > d.day ? 1
                : day < d.day ? -1
                        : 0;
    }

}

  作为一位过来人也是希望大家少走一些弯路

在这里我给大家分享一些自动化测试前进之路的必须品,希望能对你带来帮助。

(软件测试相关资料,自动化测试相关资料,技术问题答疑等等)

相信能使你更好的进步!

点击下方小卡片

【自动化测试交流】:574737577(备注ccc)http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=qxJZAmfxpSO2iG5DiXEmovrrbqX9lf9Y&authKey=LtqIFckKO2IOCAiI9MaslOjsWuQVoVKRPzUSJH%2F5I9YFBuKr75dUnznAqyCpJ0t6&noverify=0&group_code=574737577

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值