Java工具集-支持各种类型快速排序工具

简单工具类

写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦
网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是
发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些
甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用
每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能
做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独
使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用.
抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!

介绍

遵从两大原则

  • 1.绝不依赖JDK以外的源码
  • 2.牺牲代码复用性,每个类都必须是单独的组件,绝不互相引用,做到完全解耦
代码示例
package com.simple.util.math;

/**
 * @program: simple_tools
 * @description: 支持各种类型快速排序工具
 * @author: ChenWenLong
 * @create: 2019-11-15 09:45
 **/
public class QuickSortUtils {

    /**
     * 功能描述:
     * 〈对指定数组进行分割〉
     *
     * @params : [a, p, r]
     * @return : int
     * @author : cwl
     * @date : 2019/11/15 9:42
     */
    private static int partitionInt(int a[],int p,int r){
        int x = a[r-1];
        int i = p - 1;
        int temp;
        for(int j=p;j<=r-1;j++){
            if(a[j-1]<=x){
                i++;
                temp = a[j-1];
                a[j-1] = a[i-1];
                a[i-1] = temp;
            }
        }
        temp = a[r-1];
        a[r-1] = a[i];
        a[i] = temp;
        return i+1;
    }

    /**
     * 功能描述:
     * 〈double类型快速排序〉
     *
     * @params : [a, p, r]
     * @return : int
     * @author : cwl
     * @date : 2019/11/15 9:47
     */
    private static int partitionDouble(double a[],int p,int r){
        double x = a[r-1];
        int i = p - 1;
        double temp;
        for(int j=p;j<=r-1;j++){
            if(a[j-1]<=x){
                i++;
                temp = a[j-1];
                a[j-1] = a[i-1];
                a[i-1] = temp;
            }
        }
        temp = a[r-1];
        a[r-1] = a[i];
        a[i] = temp;
        return i+1;
    }

    /**
     * 功能描述:
     * 〈Long类型快速排序〉
     *
     * @params : [a, p, r]
     * @return : int
     * @author : cwl
     * @date : 2019/11/15 9:49
     */
    private static int partitionLong(long a[],int p,int r){
        long x = a[r-1];
        int i = p - 1;
        long temp;
        for(int j=p;j<=r-1;j++){
            if(a[j-1]<=x){
                i++;
                temp = a[j-1];
                a[j-1] = a[i-1];
                a[i-1] = temp;
            }
        }
        temp = a[r-1];
        a[r-1] = a[i];
        a[i] = temp;
        return i+1;
    }

    /**
     * 功能描述:
     * 〈快速排序方法〉
     *
     * @params : [a, p, r]
     * @return : void
     * @author : cwl
     * @date : 2019/11/15 9:42
     */
    public static void quickSortInt(int a[],int p,int r){
        if(p < r){
            int q = partitionInt(a, p, r);
            quickSortInt(a, p, q-1);
            quickSortInt(a, q+1, r);
        }
    }

    /**
     * 功能描述:
     * 〈double类型快排〉
     *
     * @params : [a, p, r]
     * @return : void
     * @author : cwl
     * @date : 2019/11/15 9:48
     */
    public static void quickSortDouble(double a[],int p,int r){
        if(p < r){
            int q = partitionDouble(a, p, r);
            quickSortDouble(a, p, q-1);
            quickSortDouble(a, q+1, r);
        }
    }

    /**
     * 功能描述:
     * 〈Long类型快排〉
     *
     * @params : [a, p, r]
     * @return : void
     * @author : cwl
     * @date : 2019/11/15 9:51
     */
    public static void quickSortLong(double a[],int p,int r){
        if(p < r){
            int q = partitionDouble(a, p, r);
            quickSortDouble(a, p, q-1);
            quickSortDouble(a, q+1, r);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值