【恋上数据结构】计数排序

简介: 【恋上数据结构】计数排序

我的《恋上数据结构》源码(第1季 + 第2季):https://github.com/szluyu99/Data_Structure_Note

经典的十大排序算法!
在这里插入图片描述

前言

请==务必==看一下这个:排序算法前置知识+代码环境准备

当上面的内容都准备好以后,那就开始计数排序吧!

计数排序-简单实现

之前学习的冒泡、选择、堆插入归并快速希尔排序都是基于比较的排序。

计数排序、桶排序、基数排序,都不是基于比较的排序。

  • 它们是典型的用空间换时间,在某些时候,平均时间复杂度可以比 O(nlogn) 更低

计数排序于1954年由Harold H. Seward提出,适合 对一定范围内的==整数== 进行排序。

计数排序的核心思想:

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

实现步骤

思路:

  • 首先找出要排序数组的中的最大值 max;
  • 根据最大值 max 开辟 max + 1 空间的数组 counts;
  • counts 数组统计每个整数出现的次数;
  • 根据整数出现的次数,对整数进行排序:

    • 遍历 counts 数组,索引index即为元素值counts[index] 为元素的出现次数
    • 将 counts[index] 个 index 依次放入数组,即为排序过的序列

例如对 { 7, 3, 5, 8, 7, 4, 5 } 进行计数排序:max = 8

  • counts 数组为 [0, 0, 0, 1, 1, 2, 1, 2, 1]
  • 对 index = 3, counts[index] = 1,即往新数组中放入 1 个 3
    此时新数组为 [3]
  • 对 index = 4, counts[index] = 1,即往新数组中放入 1 个 4
  • 此时新数组为 [3, 4]
  • 对 index = 5, counts[index] = 2,即往新数组中放入 2 个 5
  • 此时新数组为 [3, 4, 5, 5]
  • 对 index = 6, counts[index] = 1,即往新数组中放入 1 个 6
    此时新数组为 [3, 4, 5, 5, 6]
  • 对 index = 7, counts[index] = 2,即往新数组中放入 2 个 7
    此时新数组为 [3, 4, 5, 5, 6, 7, 7]
  • 对 index = 8, counts[index] = 1,即往新数组中放入 1 个
    此时新数组为 [3, 4, 5, 5, 6, 7, 7, 8]

在这里插入图片描述

代码实现与缺点

/**
 * 计数排序最简单的实例
 */
private void sort0(){
    // 找出最大值
    int max = array[0];
    for(int i = 1; i < array.length; i++){
        if(array[i] > max){
            max = array[i];
        }
    }
    // 根据最大值来开辟内存空间
    int[] counts = new int[max + 1];
    // 统计每个整数出现的次数
    for(int i = 0; i < array.length; i++){
        counts[array[i]]++;
    }
    // 根据整数出现的次数,对整数进行排序
    int index = 0;
    for(int i = 0; i < counts.length; i++){
        while(counts[i]-- > 0){
            array[index++] = i;
        }
    }
}

这个版本的实现存在以下问题

  • 无法对负整数进行排序
  • 极其浪费内存空间
  • 是个不稳定的排序
  • ......

计数排序 – 改进

改进思路是求出最小值
在这里插入图片描述

改进-图解

每个元素累加上其前面的所有元素得到的就是元素在有序序列中的位置信息。

例如对 { 7, 3, 5, 8, 6, 7, 4, 5 } 进行改进过的计数排序:min = 3,max = 8
newArray 为新开辟的数组,newArray = [0, 0, 0, 0, 0, 0, 0, 0],用来存放排序结果

  • counts = [1, 2, 4, 5 ,7, 8];

元素 5 在有序序列中的的索引:counts[5 - 3] - 1 = counts[2] - 1 = 3
newArray[3] = 5; 此时 newArray = [0, 0, 0, 5, 0, 0, 0, 0]
counts[2]--;
在这里插入图片描述

  • counts = [1, 2, 3, 5 ,7, 8];

元素 4 在有序序列中的的索引:counts[4 - 3] - 1 = counts[1] - 1 = 1
newArray[1] = 4; 此时 newArray = [0, 4, 0, 5, 0, 0, 0, 0]
counts[1]--;

  • counts = [1, 1, 3, 5 ,7, 8];

元素 7 在有序序列中的的索引:counts[7 - 3] - 1 = counts[4] - 1 = 6
newArray[6] = 7; 此时 newArray = [0, 4, 0, 5, 0, 0, 7, 0]
counts[4]--;
在这里插入图片描述

  • counts = [1, 1, 3, 5 ,6, 8];

元素 6 在有序序列中的的索引:counts[6 - 3] - 1 = counts[3] - 1 = 4
newArray[4] = 6; 此时 newArray = [0, 4, 0, 5, 6, 0, 7, 0]
counts[3]--;

  • counts = [1, 1, 3, 4 ,6, 8];
    元素 8 在有序序列中的的索引:counts[8 - 3] - 1 = counts[5] - 1 = 7
    newArray[7] = 8; 此时 newArray = [0, 4, 0, 5, 6, 0, 7, 8]

counts[5]--;
在这里插入图片描述

  • counts = [1, 1, 3, 4 ,6, 7];

元素 5 在有序序列中的的索引: counts[5 - 3] -1 = counts[2] - 1 = 2
newArray[2] = 5; 此时 newArray = [0, 4, 5, 5, 6, 0, 7, 8]
counts[2]--;

  • counts = [1, 1, 2, 4 ,6, 7];

元素 3 在有序序列中的的索引:counts[3 - 3] - 1 = counts[0] - 1 = 0
newArray[0] = 3; 此时 newArray = [3, 4, 5, 5, 6, 0, 7, 8]
counts[0]--;
在这里插入图片描述

  • counts = [0, 1, 2, 4 ,6, 7];

元素 7 在有序序列中的的索引:counts[7 - 3] - 1 = counts[4] - 1 = 5
newArray[5] = 8; 此时 newArray = [3, 4, 5, 5, 6, 7, 7, 8]
counts[5]--;
在这里插入图片描述

改进-实现

/**
 * 计数排序
 */
public class CountingSort extends Sort<Integer>{
    
    @Override
    protected void sort() {
        // 找出最值
        int max = array[0];
        int min = array[0];
        for(int i = 0; i < array.length; i++){
            if(array[i] < min){
                min = array[i];
            }
            if(array[i] > max){
                max = array[i];
            }
        }
        // 开辟内存空间,存储次数
        int[] counts = new int[max - min + 1];
        // 统计每个整数出现的次数
        for(int i = 0; i < array.length; i++){
            counts[array[i] - min]++;
        }
        // 累加次数
        for(int i = 1; i < counts.length; i++){
            counts[i] += counts[i - 1];
        }
        // 从后往前遍历元素,将它放到有序数组中的合适位置
        int[] newArray = new int[array.length];
        for(int i = array.length - 1; i >= 0; i--){
            newArray[--counts[array[i] - min]] = array[i];
        }
        // 将有序数组赋值到array
        for (int i = 0; i < newArray.length; i++) {
            array[i] = newArray[i];
        }
    }
    
}

复杂度与稳定性

  • 最好、最坏、平均时间复杂度:O(n + k)
  • 空间复杂度:O(n + k)
  • k 是整数的取值范围
  • 计数排序属于稳定排序

计数排序-对自定义对象进行排序

如果自定义对象可以提供用以排序的整数类型,依然可以使用计数排序

private static class Person {
    int age;
    String name;
    Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [age=" + age 
                + ", name=" + name + "]";
    }
}
public static void main(String[] args) {
    Person[] persons = new Person[] {
            new Person(20, "A"),
            new Person(-13, "B"),
            new Person(17, "C"),
            new Person(12, "D"),
            new Person(-13, "E"),
            new Person(20, "F")
    };
    // 找出最值
    int max = persons[0].age;
    int min = persons[0].age;
    for (int i = 1; i < persons.length; i++) {
        if (persons[i].age > max) {
            max = persons[i].age;
        }
        if (persons[i].age < min) {
            min = persons[i].age;
        }
    }
    // 开辟内存空间,存储次数
    int[] counts = new int[max - min + 1];
    // 统计每个整数出现的次数
    for (int i = 0; i < persons.length; i++) {
        counts[persons[i].age - min]++;
    }
    // 累加次数
    for (int i = 1; i < counts.length; i++) {
        counts[i] += counts[i - 1];
    }
    // 从后往前遍历元素,将它放到有序数组中的合适位置
    Person[] newArray = new Person[persons.length];
    for (int i = persons.length - 1; i >= 0; i--) {
        newArray[--counts[persons[i].age - min]] = persons[i];
    }
    // 将有序数组赋值到array
    for (int i = 0; i < newArray.length; i++) {
        persons[i] = newArray[i];
    }
        
    for (int i = 0; i < persons.length; i++) {
        System.out.println(persons[i]);
    }
}

排序结果:

Person [age=-13, name=B]
Person [age=-13, name=E]
Person [age=12, name=D]
Person [age=17, name=C]
Person [age=20, name=A]
Person [age=20, name=F]
相关文章
|
4月前
|
搜索推荐 算法 测试技术
数据结构排序——计数排序和排序总结(附上912. 排序数组讲解)
数据结构排序——计数排序和排序总结(附上912. 排序数组讲解)
46 0
|
1月前
|
存储 搜索推荐 算法
【初阶数据结构篇】归并排序和计数排序(总结篇)
归并排序(MERGE-SORT)是建⽴在归并操作上的⼀种有效的排序算法,该算法是采⽤分治法(Divide andConquer)的⼀个⾮常典型的应⽤。
|
3月前
|
算法 搜索推荐
数据结构算法--6 希尔排序和计数排序
**希尔排序**是插入排序的改进版,通过分组插入来提高效率。它逐步减少元素间的间隔(增量序列),每次对每个间隔内的元素进行插入排序,最终增量为1时进行最后一次直接插入排序,实现整体接近有序到完全有序的过程。例如,对数组`5, 7, 4, 6, 3, 1, 2, 9, 8`,先以间隔`d=4`排序,然后`d=2`,最后`d=1`,完成排序。计数排序则适用于0到100的数值,通过统计每个数出现次数,创建对应计数数组,再根据计数重建有序数组,时间复杂度为`O(n)`。
|
3月前
|
存储 算法 搜索推荐
【数据结构】归并排序的非递归写法和计数排序
【数据结构】归并排序的非递归写法和计数排序
|
4月前
|
存储 人工智能 算法
[数据结构]——非比较排序—计数排序
[数据结构]——非比较排序—计数排序
|
4月前
数据结构——lesson13排序之计数排序
数据结构——lesson13排序之计数排序
|
4月前
|
搜索推荐 C语言
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
33 0
|
4月前
|
存储 搜索推荐 算法
【数据结构】八大排序之计数排序算法
【数据结构】八大排序之计数排序算法
31 4
|
4月前
|
搜索推荐 算法 Java
【数据结构排序算法篇】----计数排序【实战演练】
【数据结构排序算法篇】----计数排序【实战演练】
46 2
|
4月前
|
存储 搜索推荐 算法
【数据结构】归并排序的两种实现方式与计数排序
【数据结构】归并排序的两种实现方式与计数排序