Java算法,排序,对比

1、数组对比排序,eg:  {5,2,0,1,3,7,4,9,8} ,

@Test
public void testArray(){
    int[] array = new int[]{5,2,0,1,3,7,4,9,8};
    for (int i=0;i<array.length-1;i++){
        int index = i;
        for (int j=index+1;j<array.length;j++){
            if (array[index] > array[j]){ // 判断对比,如果成立,就把下标替换
                index = j;
            }
        }

        if (index != i){                   
            int temp = array[index];       // 一组对比结束之后,把数字较小的数字赋给temp 
            array[index] = array[i];       // 把当前作为对比的那个固定的数字赋给(替换)一组对比中数字最小的位置上
            array[i] = temp;               // 把最小的数字temp在赋给当前对比的固定的位置上
        }
    }
    for (int i : array) {
        System.out.print(i+" ");            //打印排序好的数组
    }
}

输出:0 1 2 3 4 5 7 8 9  

2、Comparable对比的使用,

CompaireNum[] num = new CompaireNum[]{
        new CompaireNum(2018,8),new CompaireNum(2018,7),new CompaireNum(2018,4),new CompaireNum(2018,6),
        new CompaireNum(2018,11),new CompaireNum(2018,2)

对比上面的进行排序,可以按年,按月排序

1)、定义一个类CompaireNum 

import android.support.annotation.NonNull;

public class CompaireNum implements Comparable  {

    public int year;
    public int mouth;

    public CompaireNum(int year, int mouth) {
        this.year = year;
        this.mouth = mouth;
    }

    @Override
    public int compareTo(@NonNull Object o) {

        CompaireNum num = (CompaireNum) o;

        if (this.year > num.year){
            return 1;
        }else if (this.year < num.year){
            return -1;
        }

        if (this.mouth > num.mouth){
            return 1;
        }else if (this.mouth < num.mouth){
            return -1;
        }

        return 0;
    }

    @Override
    public String toString() {
        return "CompaireNum{" +
                "year=" + year +
                ", mouth=" + mouth +
                '}';
    }
}

2)、接着开始对比

@Test
public void compaireArray(){
    CompaireNum[] num = new CompaireNum[]{
            new CompaireNum(2018,8),new CompaireNum(2018,7),new CompaireNum(2018,4),new CompaireNum(2018,6),
            new CompaireNum(2018,11),new CompaireNum(2018,2)
    };
//上面的对象数字可以修改,对比先按年,在按月进行从小到大

    // 对比
    for (int i = num.length -1; i > 0 ; i--) {
        boolean isStart = true;
        for (int j = 0; j < i ; j++) {          
            if (num[j].compareTo(num[j+1]) > 0){ // 这个对象在CompaireNum 中已做对比,
                CompaireNum compaireNum = num[j]; // 这个里面逻辑和上面的单个数字数组对比一样,
                num[j] = num[j+1];
                num[j+1] = compaireNum;
                isStart = false;
            }
        }
        if (isStart){
            break;
        }
    }

    for (CompaireNum compaireNum:num) {
        System.out.println(  "    " +compaireNum);  // 这里打印
    }
}

打印结果:

    CompaireNum{year=2018, mouth=2}
    CompaireNum{year=2018, mouth=4}
    CompaireNum{year=2018, mouth=6}
    CompaireNum{year=2018, mouth=7}
    CompaireNum{year=2018, mouth=8}
    CompaireNum{year=2018, mouth=11}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值