`

Counting Sort

 
阅读更多

Let us understand it with the help of an example.

For simplicity, consider the data in the range 0 to 9. 
Input data: 1, 4, 1, 2, 7, 5, 2
  1) Take a count array to store the count of each unique object.
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  2  0   1  1  0  1  0  0

  2) Modify the count array such that each element at each index 
  stores the sum of previous counts. 
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  4  4  5  6  6  7  7  7

The modified count array indicates the position of each object in 
the output sequence.
 
  3) Output each object from the input sequence followed by 
  decreasing its count by 1.
  Process the input data: 1, 4, 1, 2, 7, 5, 2. Position of 1 is 2.
  Put data 1 at index 2 in output. Decrease count by 1 to place 
  next data 1 at an index 1 smaller than this index.

Following is C implementation of counting sort.

/* end is the last index + 1 */
void csort(int array[], const int end, const int max, const int min)
{
  int i;
  const int range = max-min+1;
  int count[range+1],
      scratch[end];
 
  for(i=0; i<range+1; i++)
    count[i] = 0;
 
  /* Set the value of count[i] to the number of
   * elements in array with value i+min-1. */
  for(i=0; i<end; i++) {
    int c = array[i]+1-min;
    count[c]++;
  }
 
  /* Update count[i] to be the number of
   * elements with value less than i+min. */
  for(i=1; i<range; i++)
    count[i] + = count[i-1];
 
  /* Copy the elements of array into scratch in
   * stable sorted order. */
  for(i=(end-1); i>=0; i--) {
    int c = array[i]-min;
    int s = count[c];
    scratch[s] = array[i];
    /* Increment count so that the next element
     * with the same value as the current element
     * is placed into its own position in scratch. */
    count[c]++;
  }
 
  for(i=0; i<end; i++)
    array[i] = scratch[i];
}

 

public void countingSort(int[] a, int low, int high) {
    int[] counts = new int[high - low + 1]; // this will hold all possible values, from low to high
    for (int x : a)
        counts[x - low]++; // - low so the lowest possible value is always 0
 
    int current = 0;
    for (int i = 0; i < counts.length; i++) {
        Arrays.fill(a, current, current + counts[i], i + low); // fills counts[i] elements of value i + low in current
        current += counts[i]; // leap forward by counts[i] steps
    }
}

 

这里可以查看Counting Sort的动画演示。

http://www.cs.miami.edu/~burt/learning/Csc517.101/workbook/countingsort.html

 

References:

http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Counting_sort

http://www.geeksforgeeks.org/counting-sort/

分享到:
评论

相关推荐

    AlgorithmMan by Iori(Counting Sort)

    CountingSort为AlgorithmMan中的计数排序演示工具(这是可执行文件;需要.net 4.0支持;非源代码)。 原文:C#算法设计排序篇之08-计数排序(附带动画演示程序) 链接:...

    排序算法-基于C语言实现的排序算法之CountingSort实现.zip

    **C语言实现CountingSort:** C语言的简洁和高效使得它成为实现算法的理想选择。以下是一个简单的Counting Sort C语言实现的框架: ```c #include void countingSort(int arr[], int n) { // 步骤1和2:初始化...

    计数排序JAVA实现counting sort algorithm

    public static int[] countingSort(int[] arr) { // Step 1: 获取数组最大值 int max = Arrays.stream(arr).max().getAsInt(); // Step 2: 初始化计数数组 int[] countArray = new int[max + 1]; // Step ...

    排序算法之CountingSort

    python 排序算法之CountingSort

    CountingSort:计数排序算法的实现

    var countingSort = require ( './countingSort.js' ) ; // Construct input [0, 5], therefore an array of size 6 is needed // for the temporary storage space. var input = [ 2 , 5 , 3 , 0 , 2 , 3 , 0 , 3 ...

    countingSort:计数排序是一种基于特定范围内的键的排序技术。 它通过计算具有不同键值(有点像哈希)的对象数来工作

    function countingSort(arr) { const min = Math.min(...arr); const max = Math.max(...arr); const count = new Array(max - min + 1).fill(0); arr.forEach(num =&gt; count[num - min]++); const sortedArr...

    radix sort

    在这个例子中,`countingSort`函数实现了计数排序,`radixsort`函数负责调用`countingSort`并处理从低位到高位的每一位。这段代码适用于整数数组的排序,如果需要处理浮点数或者负数,还需要进行适当的修改。 总结...

    python 实现 排序 课程设计 代码

    计数排序(Counting Sort) 循环排序(Cycle Sort) 双重排序(Double Sort) 荷兰国旗排序(Dutch National Flag Sort) 交换排序(Exchange Sort) 外部排序(External Sort) 侏儒排序(Gnome Sort) 堆排序...

    matlab开发-sort1m

    7. **计数排序(Counting Sort)、桶排序(Bucket Sort)、基数排序(Radix Sort)**:这些是针对特定类型数据(如非负整数)的排序算法,可以在一定条件下达到线性时间复杂度。 "sort1.m"的实现可能考虑了算法效率...

    count_sort.zip_count_sort_in

    在`main`函数中,我们创建了一个测试数组并调用`countingSort`函数进行排序,最后打印出排序后的结果。 需要注意的是,计数排序不是稳定的排序算法,即相等的元素可能会改变它们原来的相对顺序。此外,当元素范围很...

    sort.cpp_排序算法演示程序_

    7. **计数排序(Counting Sort)**:非基于比较的排序算法,适用于整数排序,尤其在数据范围不大的情况下效率极高。 在"sort.cpp"中,每种排序算法的实现都可能包含关键步骤的注释,这对于初学者理解各种排序算法的...

    经典算法的C#源码实现

    经典的排序算法C#源码,包括: 经典排序算法 - 快速排序Quick sort 经典排序算法 - 桶排序Bucket sort 经典排序算法 - 插入排序Insertion sort 经典排序算法 - 基数排序...经典排序算法 - 计数排序Counting sort

    CSort内排序算法

    7. **计数排序(Counting Sort)**: 计数排序是一种非基于比较的排序算法,适用于待排序的元素范围不大的情况。它通过统计每个元素出现的次数,然后计算出每个元素应该在输出序列中的位置。其时间复杂度为O(n+k),...

    常用的8个Python排序算法

    1. 冒泡排序(Bubble Sort) def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] &gt; arr[j+1]: ...8. 计数排序(Counting Sort) 解压密码 douge

    java实现计数排序算法

    countingSort 方法实现了计数排序算法。首先,我们遍历一遍原数组,找出其中的最大值,以确定计数数组的大小。然后,创建计数数组 count,并将每个元素的出现次数记录在计数数组中。接下来,根据计数数组中的元素...

    常用的十种java排序算法实现

    1. 冒泡排序(Bubble Sort) public static void bubbleSort(int...7. 计数排序(Counting Sort) 8. 桶排序(Bucket Sort) 9. 基数排序(Radix Sort) 10. 希尔排序(Shell Sort) 解压密码 douge

    Sort源代码

    7. 计数排序(Counting Sort):非基于比较的排序算法,适用于整数排序,通过统计每个元素出现的次数,直接确定每个元素的位置。 8. 桶排序(Bucket Sort):当输入数据服从均匀分布时,桶排序非常有效。将数据分配...

    排序算法大全

    Rank Sort是Comparison Counting Sort的变形,省略了资料排名阵列,而在每一次需要时再去扫瞄资料算出,因此速度更慢。然而,这个程式并没有考虑到键值相同的情况,因此并不实用。 Distribution Counting Sort也是 ...

    算法编程题 排序、搜索、图论、动态规划

    计数排序(Counting Sort) 桶排序(Bucket Sort) 基数排序(Radix Sort) 搜索算法 线性搜索(Linear Search) 二分搜索(Binary Search) 深度优先搜索(Depth-First Search) 广度优先搜索(Breadth-First ...

    count_sort.rar_count_sort

    **计数排序(Counting Sort)**是一种非基于比较的排序算法,它适用于待排序的数据是整数,并且数值范围相对较小的情况。计数排序的基本思想是统计每个输入元素x在范围内出现的次数,然后根据这些计数来确定每个元素在...

Global site tag (gtag.js) - Google Analytics