对于一个对象数组进行

排序 和 查找

 

package cn.wry.oop_test;

public class Person implements Comparable<Person>{
    private String name;
    private int age;
    private String job;

    public Person(String name, int age, String job){
        this.name = name;
        this.age = age;
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }

    //自定义排序方法 小到大
    @Override
    public int compareTo(Person o) {
        return this.age-o.age;
    }
}

 

冒泡排序        二分查找

package cn.wry.oop_test;

import java.util.Arrays;

public class PersonMain {
    public static void main(String[] args) {
        PersonMain person = new PersonMain();
        Person[] p = new Person[4];
        p[0] = new Person("张三", 20, "学生");
        p[1] = new Person("李四", 18, "社会人");
        p[2] = new Person("王五", 22, "教师");
        p[3] = new Person("老六", 28, "歌手");

//        Arrays.sort(p);
//        System.out.println(Arrays.toString(p));
        Person[] people = person.maoPao(p);
        System.out.println(Arrays.toString(people));


        int index = person.binarySearch2(p, 22);
        if (index > 0) {
            System.out.println("找到了,索引值为:" + index);
        }else {
            System.out.println("不存在这个age");
        }
    }
//根据年龄实现冒泡排序(降序)
    public Person[] maoPao(Person p[]){
        for (int i = 0; i < p.length - 1; i++) {
            for (int j = 0; j < p.length - 1 - i; j++) {
                if (p[j].getAge() < p[j+1].getAge()){
                    Person temp = p[j];
                    p[j] = p[j+1];
                    p[j+1] = temp;
                }
            }
        }
        return p;
    }

    //二分查找 由大到小排序查找
    public int binarySearch2(Person[] p, int age) {
        int min = p.length - 1;
        int max = 0;
        if (p.length == 0|| max > min||p[min].getAge() > age || p[max].getAge() < age) {
            return -1;
        }
        while (max <= min) {
                int mid = (min + max) / 2;
                if (p[mid].getAge() > age) {
                    max = mid + 1;
                } else if (p[mid].getAge() < age) {
                    min = mid - 1;
                } else {
                    return mid;
                }
        }
        return -1;
    }


//二分查找 由小到大排序查找
    public int binarySearch(Person[] p, int age) {
        int min = 0;
        int max = p.length - 1;
        if (p.length == 0|| max < min ||p[min].getAge() > age || p[max].getAge() < age) {
            return -1;
        }
        while (max>=min){
            int mid = (min+max)/2;
            if (p[mid].getAge()> age){
                max = mid - 1;
            }else if (p[mid].getAge() <age){
                min = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }

}