English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Assume n elements in range 1 to k. When k = O (n), running time is O (n).

Arrays:

A[1 ... n] original unsorted array
B[1 ... n] array to hold sorted output
C[1 ... k] working array to hold counts

Counting-Sort ( A, B, k )

for i ¬ 1 to k
do C[i] ¬ 0

for j ¬ 1 to length[A]
do C[A[j]] ¬ C[A[j]] + 1

// C[i] now contains the number of elements equal to k.

for i ¬ 2 to k
do C[i] ¬ C[i] + C[i-1]

// C[i] now contains the number of elements less than or equal to i

for j ¬ length[A] downto 1
do B[C[A[j]]] ¬ A[j]
C[A[j]] ¬ C[A[j]] - 1

2006-10-24 06:10:07 · 4 answers · asked by Wattsie 3 in Computers & Internet Programming & Design

4 answers

I would ask the people at codeguru.com

2006-10-24 07:53:56 · answer #1 · answered by Siu02rk 3 · 0 0

in the part that calculates the number of elements less than or equal to c[i] shouldn't it be:

for i = 0 to k
if a[i] <= k then
c[i] = c[i] + 1

2006-10-24 13:26:05 · answer #2 · answered by P_Dogg 2 · 0 0

Means nowt to me, sorry.

2006-10-24 13:29:04 · answer #3 · answered by fuck off 5 · 0 0

your asking to much

2006-10-24 13:12:27 · answer #4 · answered by blah blah blah 3 · 1 0

fedest.com, questions and answers