# counting sort

COUNTING-SORT(A, B, k)

for i <- 0 to k

do C[i] <- 0

for j <- 1 to length[A]

do C[A[j]] <- C[A[j]] +1

for i <- 1 to k

do C[i] <- C[i] + C[i-1]

for j <- length[A] downto 1

do B[C[A[j]]] <- A[j]

C[A[j]] <- C[A[j]] -1

#include <stdio.h>

void countingsort(int a[], int b[], int len, int k)
{
int i;
int c[k+1];

for (i = 0; i <= k; i++) {
c[i] = 0;
}

for (i = 1; i <= len; i++ ) {
c[a[i]]++;
}

for (i = 1; i <= k; i++) {
c[i] += c[i-1];
}

for (i = len; i>= 1; i--) {
b[c[a[i]]] = a[i];
c[a[i]]--;
}
}

int main(void)
{
int i;
int A[] = {
0, 2, 5, 3, 0, 2, 3, 0, 3
};
int B[9];

countingsort(A, B, 8, 5);

for (i = 1; i < 9; i++) {
printf("%d ", B[i]);
}

return 0;
}


2017-05-21 10:39:14

#### Counting Sort 其實很簡單

2016-08-25 21:34:52

#### 计数排序CountingSort

2018-06-14 17:01:20

#### 计数排序Counting sort

2017-01-11 22:29:00

#### 算法数据结构C++实现4-计数排序(counting sort)

2013-10-08 16:08:31

#### 无聊写排序之 ---- 计数排序(CountingSort)

2015-01-30 11:34:31

#### 计数排序countingsort

2016-04-17 21:00:38

#### 计数排序 （Counting Sort）

2013-06-18 18:39:41

#### 计数排序（Counting Sort）

2015-02-22 16:12:50

#### 计数排序（CountingSort）

2016-04-03 02:24:23