Internal Sorting: Distribution counting: Sorting by counting

本文详细介绍了分布计数排序(Algorithm D),一种适用于整数排序的内部排序算法。通过辅助计数表COUNT对记录进行排序,首先清零COUNT数组,然后遍历记录增加对应计数,接着累积计数,最后根据计数输出排序记录。同时提供了算法流程图和Java程序示例,并引用了Donald E. Knuth的《计算机程序设计艺术:排序与搜索》作为参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Distribution counting sort:分布计数排序


Algorithm D

Algorithm D (Distribution counting). Assuming that all keys are integers in
the range u<=Kj<=v for 1<j<N , this algorithm sorts the records R1,...,RN
by making use of an auxiliary table COUNT[u],...,COUNT[v] . At the conclusion
of the algorithm the records are moved to an output area S1,...,SN in the
desired order.
D1. [Clear COUNTs.] Set COUNT[u] through COUNT[v] all to zero.
D2. [Loop on j.] Perform step D3 for 1<=j<=N ; then go to step D4.
D3. [Increase COUNT[ Kj ].] Increase the value of COUNT[Kj] by 1 .
D4. [Accumulate.] (At this point COUNT[i] is the number of keys that are equal
to i .) Set COUNT[i]COUNT[i]+COUNT[i1] , for i=u+1,u+2,...,v .
D5. [Loop on j.] (At this point COUNT[i] is the number of keys that are less than
or equal to i ; in particular, COUNT[v]=N.) Perform step D6 for j=N,N1,,1 ; then terminate the algorithm.
D6. [Output Rj] Set iCOUNT[Kj],SiRj, and COUNT[Kj]i1 . |


Flow diagram

Distribution counting:Sorting by counting:Internal Sorting


Java program

In thisprogram, R1,…,RN were simplified to K1,…,KN.

/**
 * Created with IntelliJ IDEA.
 * User: 1O1O
 * Date: 11/19/13
 * Time: 10:01 PM
 * :)~
 * Distribution counting:Sorting by counting:Internal Sorting
 */
public class Main {

    public static void main(String[] args) {
        int N = 16;
        int[] S = new int[17];
        int[] keys = new int[17];
        int[] COUNT = new int[62];
        int u = 3;
        int v = 61;

        /*Prepare the data,3<=keys<=61*/
        keys[1] = 3;
        keys[2] = 57;
        keys[3] = 12;
        keys[4] = 61;
        keys[5] = 8;
        keys[6] = 17;
        keys[7] = 7;
        keys[8] = 25;
        keys[9] = 53;
        keys[10] = 42;
        keys[11] = 14;
        keys[12] = 9;
        keys[13] = 12;
        keys[14] = 46;
        keys[15] = 50;
        keys[16] = 27;

        /*Output unsorted keys*/
        System.out.println("Unsorted keys:");
        for(int i=1; i<=N; i++){
            System.out.println(i+":"+keys[i]);
        }
        System.out.println();

        /*Clear COUNTs*/
        for(int i=u; i<=v; i++){
            COUNT[i] = 0;
        }

        /*Kernel of the Algorithm*/
        for(int j=1; j<=N; j++){
            COUNT[keys[j]]++;
        }
        for(int i=u+1; i<=v; i++){
            COUNT[i] = COUNT[i] + COUNT[i-1];
        }
        for(int j=N; j>=1; j--){
            int i = COUNT[keys[j]];
            S[i] = keys[j];
            COUNT[keys[j]] = i-1;
        }

        /*Output sorted keys*/
        System.out.println("Sorted keys:");
        for(int i=1; i<=N; i++){
            System.out.println(i+":"+S[i]);
        }
    }
}

Outputs

Unsorted keys:
1:3
2:57
3:12
4:61
5:8
6:17
7:7
8:25
9:53
10:42
11:14
12:9
13:12
14:46
15:50
16:27

Sorted keys:
1:3
2:7
3:8
4:9
5:12
6:12
7:14
8:17
9:25
10:27
11:42
12:46
13:50
14:53
15:57
16:61

Reference

<< The art of computer programming: Sorting and Searching >> VOLUME 3, DONALD E. KNUTH
https://zh.wikipedia.org/wiki/%E8%AE%A1%E6%95%B0%E6%8E%92%E5%BA%8F
https://en.wikipedia.org/wiki/Counting_sort

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值