不是元素不能比较 而是不借助compareTo进行实现

计数排序

统计每个整数在序列中出现的次数进而推导出每个整数在有序列中的索引

  1. public class CountSort {
  2. public void sortColors(int[] nums) {
  3. //处理元素取值范围[0,R) --> R=最大值+1
  4. int R = 3;//数组中数的范围 0,1,2
  5. int[] cnt = new int[R];
  6. //cnt数组存储的就是索引位置上有多少个值 索引就是值
  7. for (int num : nums) {
  8. cnt[num]++;
  9. }
  10. //记录i值的范围 [index[i],index[i+1])]范围都是i
  11. int[] index = new int[R + 1];//index记录的是每个值的区间
  12. index[0] = 0;
  13. for (int i = 0; i < R; i++) {
  14. //前一个值+当前位置值的个数就是这个值所在的区间
  15. index[i + 1] = index[i] + cnt[i];
  16. }
  17. for (int i = 0; i + 1 < index.length; i++) {
  18. //遍历值为i 所对应的区间 index[i],index[i+1])] i+1不能越界 所以i + 1 < index.length
  19. for (int j = index[i]; j < index[i + 1]; j++) {
  20. nums[j] = i;
  21. }
  22. }
  23. }
  24. public static void main(String[] args) {
  25. int [] nums = {1,2,3,1,2,3,1,2,1,2,1,3,3};
  26. new CountSort().sortColors(nums);
  27. for (int i : nums){
  28. System.out.println(i);
  29. }
  30. }
  31. }

对对象进行排序

  1. public class CountSort {
  2. public void sortObj(StudentSore[] studentSores) {
  3. //定义分数的范围 R=最大值+1
  4. int R = 101; //[0,101);
  5. int[] count = new int[R];//保存每个分数下的个数
  6. //记录每个分数(索引)的个数
  7. for (StudentSore s : studentSores) {
  8. count[s.getScore()]++;
  9. }
  10. //[index,index+1)
  11. int[] index = new int[R + 1]; //记录每个分数的区间
  12. index[0] = 0;
  13. //把每个数的区间保存下来
  14. for (int i = 0; i < R; i++) {
  15. index[i + 1] = index[i] + count[i];//这算的是当前值结束的位置(不包含)和下一个值开始的位置
  16. }
  17. //通过这个临时对象保存排好的序
  18. StudentSore[] temp = new StudentSore[studentSores.length];
  19. /**
  20. * 原数组 2 1 0 1 2 0
  21. * A B C D E F
  22. * -----------------------------------
  23. * 0 1 2
  24. * count 2 2 2
  25. * index 0 2 4 6
  26. * 0->[0,2) 1->[2,4) 2->[4,6)
  27. */
  28. for (StudentSore s : studentSores) {
  29. temp[index[s.getScore()]] = s;
  30. index[s.getScore()]++;
  31. }
  32. //将排好序的对象放入到原始数组中
  33. for (int i = 0; i < temp.length; i++) {
  34. studentSores [i] = temp[i];
  35. }
  36. }
  37. public static void main(String[] args) {
  38. StudentSore[] studentSores = new StudentSore[26];
  39. int k = 0;
  40. for (char i = 'a'; i <= 'z'; i++) {
  41. studentSores[k] = new StudentSore(i+"", new Random().nextInt(101));
  42. k++;
  43. }
  44. new CountSort().sortObj(studentSores);
  45. for (StudentSore s : studentSores) {
  46. System.out.println(s);
  47. }
  48. }
  49. }
  50. class StudentSore {
  51. String name;
  52. int score;
  53. public StudentSore(String name, int score) {
  54. this.name = name;
  55. this.score = score;
  56. }
  57. public int getScore() {
  58. return score;
  59. }
  60. @Override
  61. public String toString() {
  62. return "StudentSore{" +
  63. "name='" + name + '\'' +
  64. ", score=" + score +
  65. '}';
  66. }
  67. }

基数排序

依次对个位数,十位数,百位数,千位数,万位数。。。。进行排序
只能对整数进行排序

public class BaseSort {

    public void baseSort(int[] nums) {
        //找到最大值
        int max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) max = nums[i];
        }
        //循环对每一位进行判断
        int base = 1;//记录当前位数
        int[] temp = new int[nums.length];//保存临时排序好的数组
        //当base位数<=max的位数时进行循环
        while (max / base > 0) {
            //将当前位数下0-9出现的次数保存在里面
            //后面将次数替换成在数组中最大长度(因为是索引存入数组中是-1)
            int[] bucket = new int[10];//不管哪一位上都是0-9 10个数字
            for (int i = 0; i < nums.length; i++) {
                //统计当前位数下0-9出现的次数
                bucket[nums[i] / base % 10]++;
            }
            //将次数替换成在数组中最大长度 --> 当前位上的值加上前面的长度=当前值在数组中的最大长度(使用时-1)
            for (int i = 1; i < bucket.length; i++) {
                //计算当前值在数组中的最大长度
                bucket[i] = bucket[i] + bucket[i - 1];
            }
            //从后往前的方式遍历数组
            //bucket[nums[i] / base % 10]当做值在数组的长度 - 1数组中的索引位置
            for (int i = nums.length - 1; i >= 0; i--) {
                temp[bucket[nums[i] / base % 10] - 1] = nums[i];
                bucket[nums[i] / base % 10]--;//有多个为数值相同 下个长度后移
            }
            //重新赋值到nums中去
            for (int i = 0; i < nums.length; i++) {
                nums[i] = temp[i];
            }
            //每次扩大10倍--->当位数比max大的时候max/base=0(1234/10000=0)
            base = base * 10;
        }
    }

    public static void main(String[] args) {
        int[] nums = {111, 2, 23, 1, 52, 3, 1, 2, 1, 2, 1, 3, 3};
        new BaseSort().baseSort(nums);
        for (int n : nums) {
            System.out.println(n);
        }
    }

桶排序

按一定规则将数据放入不同的桶中,对不同的桶进行单独排序在进行输出

public class BucketSort {

    public void buckerSort(int[] nums) {
        //找到最大值
        int max = nums[0];
        for (int i : nums) {
            if (i > max) {
                max = i;
            }
        }
        //计算最大值的长度 当做桶的长度
        int length = new String(Integer.toString(max)).length();
        List[] buckets = new ArrayList[length];
        //初始化桶
        for (int i = 0; i < buckets.length; i++)//初始化
        {
            buckets[i] = new ArrayList<Integer>();
        }
        //将数据按照某种规则放入桶中
        for (int i = 0; i < nums.length; i++)//将待排序序列放入对应桶中
        {
            //根据数组的位数放入桶中1位对应的是0号索引
            int index = new String(Integer.toString(nums[i])).length();
            buckets[index - 1].add(nums[i]);
        }
        for (int i = 0; i < buckets.length; i++) {
            buckets[i].sort(null);//使用其他排序方式对桶里的元素进行排序
            for (int j = 0; j < buckets[i].size(); j++)//顺便打印输出
            {
                System.out.print(buckets[i].get(j) + " ");
            }
        }
    }

    public static void main(String[] args) {
        new BucketSort().buckerSort(new int[]{11,22,44,1,56,11,111,1,6,7,9});
    }
}