代码01_03

88 阅读1分钟

桶排序

// only for 0~200 value
public static void countSort(int[] arr) {
    if (arr == null || arr.length < 2) {
        return;
    }
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < arr.length; i++) {
        max = Math.max(max, arr[i]);
    }
    int[] bucket = new int[max + 1];
    for (int i = 0; i < arr.length; i++) {
        bucket[arr[i]]++;
    }
    int i = 0;
    for (int j = 0; j < bucket.length; j++) {
        while (bucket[j]-- > 0) {
            arr[i++] = j;
        }
    }
}

基数排序

// only for no-negative value
public static void radixSort(int[] arr) {
    if (arr == null || arr.length < 2) {
        return;
    }
    radixSort(arr, 0, arr.length - 1, maxbits(arr));
}

public static int maxbits(int[] arr) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < arr.length; i++) {
        max = Math.max(max, arr[i]);
    }
    int res = 0;
    while (max != 0) {
        res++;
        max /= 10;
    }
    return res;
}

// arr[begin..end]排序
public static void radixSort(int[] arr, int L, int R, int digit) {
    final int radix = 10;
    int i = 0, j = 0;
    // 有多少个数准备多少个辅助空间
    int[] bucket = new int[R - L + 1];
    for (int d = 1; d <= digit; d++) { // 有多少位就进出几次
        // 10个空间
        // count[0] 当前位(d位)是0的数字有多少个
        // count[1] 当前位(d位)是(0和1)的数字有多少个
        // count[2] 当前位(d位)是(0、1和2)的数字有多少个
        // count[i] 当前位(d位)是(0~i)的数字有多少个
        int[] count = new int[radix]; // count[0..9]
        for (i = L; i <= R; i++) {
            j = getDigit(arr[i], d);
            count[j]++;
        }
        for (i = 1; i < radix; i++) {
            count[i] = count[i] + count[i - 1];
        }
        for (i = R; i >= L; i--) {
            j = getDigit(arr[i], d);
            bucket[count[j] - 1] = arr[i];
            count[j]--;
        }
        for (i = L, j = 0; i <= R; i++, j++) {
            arr[i] = bucket[j];
        }
    }
}

public static int getDigit(int x, int d) {
    return ((x / ((int) Math.pow(10, d - 1))) % 10);
}

比较排序

public static class Student {
    public String name;
    public int id;
    public int age;

    public Student(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }
}

public static class IdAscendingComparator implements Comparator<Student> {

    // 返回负数的时候,第一个参数排在前面
    // 返回正数的时候,第二个参数排在前面
    // 返回0的时候,谁在前面无所谓
    @Override
    public int compare(Student o1, Student o2) {
        return o1.id - o2.id;
    }

}

public static class IdDescendingComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o2.id - o1.id;
    }

}

public static class AgeAscendingComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }

}

public static class AgeDescendingComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o2.age - o1.age;
    }

}


public static class AgeShengIdSheng implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age != o2.age ? (o1.age - o2.age) 
            : (o1.id - o2.id);
    }

}


// 先按照id排序,id小的,放前面;
// id一样,age大的,前面;
public static class IdInAgeDe implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.id != o2.id ? o1.id - o2.id  : (  o2.age - o1.age  );
    }

}


public static void printStudents(Student[] students) {
    for (Student student : students) {
        System.out.println("Name : " + student.name + ", Id : " + student.id + ", Age : " + student.age);
    }
}

public static void printArray(Integer[] arr) {
    if (arr == null) {
        return;
    }
    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ");
    }
    System.out.println();
}

public static class MyComp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2 - o1;
    }

}


public static class AComp implements Comparator<Integer>{

    // 如果返回负数,认为第一个参数应该拍在前面
    // 如果返回正数,认为第二个参数应该拍在前面
    // 如果返回0,认为谁放前面都行
    @Override
    public int compare(Integer arg0, Integer arg1) {

        return arg1 - arg0;

        //          return 0;
    }

}