SlideShare a Scribd company logo
Counting Sort
Fernando Simeone
Mestrado em Ciência da Computação
Universidade Federal de Lavras
!
Projeto e Análise de Algoritmos (2014/2)
Tópicos
• O algoritmo
• Análise de complexidade
• Conclusão
• Referências
O algoritmo
Idéia algoritmo
Para cada elemento do array
!
Contar quantos elementos há menores do que ele
!
Inserir o elemento em sua posição correta
Entradas
Counting-Sort( input, output, k )
Entradas
Counting-Sort( input, output, k )
2 5 3 0 2 3 0 3
input
Entradas
Counting-Sort( input, output, k )
2 5 3 0 2 3 0 3
input
inteiros maiores
ou iguais a 0
Entradas
Counting-Sort( input, output, k )
2 5 3 0 2 3 0 3
input
output
inteiros maiores
ou iguais a 0
Entradas
Counting-Sort( input, output, k )
2 5 3 0 2 3 0 3
input
output
k = 5
inteiros maiores
ou iguais a 0
Entradas
Counting-Sort( input, output, k )
2 5 3 0 2 3 0 3
input
output
k = 5
inteiros maiores
ou iguais a 0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
output
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
output
1 - Inicialização
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 0 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 0 0 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 0 0 0 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
k = 5
2 5 3 0 2 3 0 3
count
0 1 2 3 4 5
output
1 - Inicialização
0 0 0 0 0 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
0 0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
0 0
j = 0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
0 0
j = 0
count[2] = count[2] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
0 0
+ 1
j = 0
1
count[2] = count[2] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
1 0
j = 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
1 0
j = 1
count[5] = count[5] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
1 0
+ 1
j = 1
1
count[5] = count[5] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
1 1
j = 2
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
1 1
j = 2
count[3] = count[3] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 0 0 0
5
output
2 - Contagem A
1 1
+ 1
j = 2
1
count[3] = count[3] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 1 0 0
5
output
2 - Contagem A
1 1
j = 3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 1 0 0
5
output
2 - Contagem A
1 1
j = 3
count[0] = count[0] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 0 0 1 0 0
5
output
2 - Contagem A
1 1
+ 1
j = 3
1
count[0] = count[0] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 1 0 0
5
output
2 - Contagem A
1 1
j = 4
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 1 0 0
5
output
2 - Contagem A
1 1
j = 4
count[2] = count[2] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 1 0 0
5
output
2 - Contagem A
1 1
+ 1
j = 4
2
count[2] = count[2] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 1 0 0
5
output
2 - Contagem A
2 1
j = 5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 1 0 0
5
output
2 - Contagem A
2 1
j = 5
count[3] = count[3] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 1 0 0
5
output
2 - Contagem A
2 1
+ 1
j = 5
2
count[3] = count[3] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 2 0 0
5
output
2 - Contagem A
2 1
j = 6
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 2 0 0
5
output
2 - Contagem A
2 1
j = 6
count[0] = count[0] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 0 0 2 0 0
5
output
2 - Contagem A
2 1
+ 1
j = 6
2
count[0] = count[0] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
2 5 3 0 2 3 0 3
O algoritmo
input
count
0 1 2 3 4
2 0 0 2 0 0
5
output
2 - Contagem A
2 1
j = 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
2 5 3 0 2 3 0 3
O algoritmo
input
count
0 1 2 3 4
2 0 0 2 0 0
5
output
2 - Contagem A
2 1
j = 7
count[3] = count[3] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
2 5 3 0 2 3 0 3
O algoritmo
input
count
0 1 2 3 4
2 0 0 2 0 0
5
output
2 - Contagem A
2 1
+ 1
j = 7
3
count[3] = count[3] + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 1
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 1
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4 7
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4 7
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4 7 7
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4 7 7
+
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 0 0 3 0 0
5
output
2 - Contagem B
2 12 4 7 7 8
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 7 7 0
5
output
3 - Ordenação
4 8
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 7 7 0
5
output
3 - Ordenação
4 8
j = 7
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 7 7 0
5
output
3 - Ordenação
4 8
- 1
j = 7
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 7 7 0
5
output
3 - Ordenação
4 8
- 1
j = 7
0 1 2 3 4 5 6 7
3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 7 7 0
5
output
3 - Ordenação
4 8
- 1
j = 7
0 1 2 3 4 5 6 7
6
3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 6 7 0
5
output
3
3 - Ordenação
4 8
j = 6
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 6 7 0
5
output
3
3 - Ordenação
4 8
- 1
j = 6
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 6 7 0
5
output
3
3 - Ordenação
4 8
- 1
j = 6
0 1 2 3 4 5 6 7
0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
2 2 0 6 7 0
5
output
3
3 - Ordenação
4 8
- 1
j = 6
0 1 2 3 4 5 6 7
1
0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 6 7 0
5
output
0 3
3 - Ordenação
4 8
j = 5
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 6 7 0
5
output
0 3
3 - Ordenação
4 8
- 1
j = 5
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 6 7 0
5
output
0 3
3 - Ordenação
4 8
- 1
j = 5
0 1 2 3 4 5 6 7
3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 6 7 0
5
output
0 3
3 - Ordenação
4 8
- 1
j = 5
0 1 2 3 4 5 6 7
5
3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 3 3
3 - Ordenação
4 8
j = 4
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 3 3
3 - Ordenação
4 8
- 1
j = 4
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 3 3
3 - Ordenação
4 8
- 1
j = 4
0 1 2 3 4 5 6 7
2
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 3 3
3 - Ordenação
4 8
- 1
j = 4
0 1 2 3 4 5 6 7
3
2
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 2 3 3
3 - Ordenação
3 8
j = 3
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 2 3 3
3 - Ordenação
3 8
- 1
j = 3
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 2 3 3
3 - Ordenação
3 8
- 1
j = 3
0 1 2 3 4 5 6 7
0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
1 2 0 5 7 0
5
output
0 2 3 3
3 - Ordenação
3 8
- 1
j = 3
0 1 2 3 4 5 6 7
0
0
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 5 7 0
5
output
0 0 2 3 3
3 - Ordenação
3 8
j = 2
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 5 7 0
5
output
0 0 2 3 3
3 - Ordenação
3 8
- 1
j = 2
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 5 7 0
5
output
0 0 2 3 3
3 - Ordenação
3 8
- 1
j = 2
0 1 2 3 4 5 6 7
3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 5 7 0
5
output
0 0 2 3 3
3 - Ordenação
3 8
- 1
j = 2
0 1 2 3 4 5 6 7
4
3
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3
3 - Ordenação
3 8
j = 1
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3
3 - Ordenação
3 8
- 1
j = 1
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3
3 - Ordenação
3 8
- 1
j = 1
0 1 2 3 4 5 6 7
5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3
3 - Ordenação
3 8
- 1
j = 1
0 1 2 3 4 5 6 7
7
5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3 5
3 - Ordenação
3 7
j = 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3 5
3 - Ordenação
3 7
- 1
j = 0
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3 5
3 - Ordenação
3 7
- 1
j = 0
0 1 2 3 4 5 6 7
2
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 3 3 3 5
3 - Ordenação
3 7
- 1
j = 0
0 1 2 3 4 5 6 7
2
2
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
0 1 2 3 4 5 6 7
O algoritmo
input
2 5 3 0 2 3 0 3
count
0 1 2 3 4
0 2 0 4 7 0
5
output
0 0 2 2 3 3 3 5
3 - Ordenação
2 7
0 1 2 3 4 5 6 7
Análise de
complexidade
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
n
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
n
k + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
n
k + 1
k
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
n
k + 1
k
n + 1
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
n
k + 1
k
n + 1
n
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input1 - Pior caso
1
k + 2
k + 1
input
count
Output
0 1 2 3 4 5 6 7
5 3 3 3 2 2 0 0
0 1 2 3 4 5 6 7
0 1 2 3 4 5
n + 1
n
k + 1
k
n + 1
n
n
Análise de complexidade
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = an + b
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = an + b
a = c4 + c5 +c8 + c9 +c10
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = an + b
a = c4 + c5 +c8 + c9 +c10
b = k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = an + b
a = c4 + c5 +c8 + c9 +c10
b = k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = O(n) + O(k)
1 - Pior caso
Análise de complexidade
c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1)
+ c9(n)+ c10(n)
n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = an + b
a = c4 + c5 +c8 + c9 +c10
b = k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8
T(n) = O(n) + O(k)
1 - Pior caso
= O(n + k)
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input2 - Melhor caso
input
0 0 2 2 3 3 3 5
0 1 2 3 4 5 6 7
Counting-sort (input, output, k)	

!
1	

 count = new int [ k ]	

!
2	

 for i = 0 to k	

3	

 	

 count[ i ] = 0	

!
4	

 for j = 0 to input.length - 1	

5	

 	

 count[ input[ j ] ] = count[ input[ j ] ] + 1	

!
6	

 for i = 1 to k	

7	

 	

 count[ i ] = count[ i ] + count[ i - 1 ]	

!
8	

 for j = A.length - 1 downto 0	

9	

 	

 output[ count[ input[ j ] ] -1 ] = input[ j ]	

10	

 	

 count[ input[ j ] ] = count[ input[ j ] ] - 1
Análise de complexidade
input2 - Melhor caso
input
0 0 2 2 3 3 3 5
0 1 2 3 4 5 6 7
O número de operações!
não depende da!
ordem inicial do array
Análise de complexidade
Análise de complexidade
T(n) = 𝛩(n + k)
Análise de complexidade
T(n) = 𝛩(n + k)
Isso faz com que o counting sort seja uma boa opção quando
k ≤ n"
pois assim seu custo será 𝛩(n).
Conclusão
Conclusão
Conclusão
• Counting Sort é um algoritmo de complexidade
linear;
Conclusão
• Counting Sort é um algoritmo de complexidade
linear;
• Não respeita o limite inferior 𝝮 (n lg n) aplicado a
algoritmos baseados em comparações;
Conclusão
• Counting Sort é um algoritmo de complexidade
linear;
• Não respeita o limite inferior 𝝮 (n lg n) aplicado a
algoritmos baseados em comparações;
• Possui complexidade O(n) quando K ≤ n;
Conclusão
• Counting Sort é um algoritmo de complexidade
linear;
• Não respeita o limite inferior 𝝮 (n lg n) aplicado a
algoritmos baseados em comparações;
• Possui complexidade O(n) quando K ≤ n;
• É um algoritmo estável, valores iguais
permanecem na mesma ordem entre si.
Referências
Referências
CORMEN, T. H.; LEISERSON, C. E.; RIVEST, R. L.; STEIN, C.
Introduction to Algorithms, Third Edition. 3rd. ed. [S.l.]: The
MIT Press, 2009. ISBN 0262033844, 9780262033848.
!
Gonzaga de Oliveira, S.L. . Algoritmos e seus fundamentos. 1.
ed. Lavras: Editora UFLA, 2011. v. 300. 440p .

More Related Content

What's hot

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Stack, Queue, Linked List.pptx
Stack, Queue, Linked List.pptxStack, Queue, Linked List.pptx
Stack, Queue, Linked List.pptx
Bharati Vidyapeeth COE, Navi Mumbai
 
Module 11
Module 11Module 11
Module 11
bittudavis
 
Domain Modeling Made Functional (KanDDDinsky 2019)
Domain Modeling Made Functional (KanDDDinsky 2019)Domain Modeling Made Functional (KanDDDinsky 2019)
Domain Modeling Made Functional (KanDDDinsky 2019)
Scott Wlaschin
 
Como fazer procedimentos no superlogo
Como fazer procedimentos no superlogoComo fazer procedimentos no superlogo
Como fazer procedimentos no superlogo
Claudia Losso
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
_fahad_shaikh
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
Bharati Vidyapeeth COE, Navi Mumbai
 
Graph.pptx
Graph.pptxGraph.pptx
Sorting Methods.pptx
Sorting Methods.pptxSorting Methods.pptx
Sorting
SortingSorting
Sorting
Jasmine Chen
 
ADA_2_Analysis of Algorithms
ADA_2_Analysis of AlgorithmsADA_2_Analysis of Algorithms
ADA_2_Analysis of Algorithms
Khushali Kathiriya
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
Mohamed Loey
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Phil Calçado
 
TUTE: Basic 0f HTML | ICT (GRADE 11 NEW) By:Raza Malhardeen©
TUTE: Basic 0f HTML | ICT (GRADE  11 NEW) By:Raza Malhardeen©TUTE: Basic 0f HTML | ICT (GRADE  11 NEW) By:Raza Malhardeen©
TUTE: Basic 0f HTML | ICT (GRADE 11 NEW) By:Raza Malhardeen©
Raza Malhardeen
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
Svetlin Nakov
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
Saqib Saeed
 
Algoritmos Gulosos - Troco Mínimo
Algoritmos Gulosos - Troco MínimoAlgoritmos Gulosos - Troco Mínimo
Algoritmos Gulosos - Troco Mínimo
Gabriel Albuquerque
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
Elaine Cecília Gatto
 
Aula 8 - Validação - parte 1
Aula 8 - Validação - parte 1Aula 8 - Validação - parte 1
Aula 8 - Validação - parte 1
Pacc UAB
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 

What's hot (20)

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Stack, Queue, Linked List.pptx
Stack, Queue, Linked List.pptxStack, Queue, Linked List.pptx
Stack, Queue, Linked List.pptx
 
Module 11
Module 11Module 11
Module 11
 
Domain Modeling Made Functional (KanDDDinsky 2019)
Domain Modeling Made Functional (KanDDDinsky 2019)Domain Modeling Made Functional (KanDDDinsky 2019)
Domain Modeling Made Functional (KanDDDinsky 2019)
 
Como fazer procedimentos no superlogo
Como fazer procedimentos no superlogoComo fazer procedimentos no superlogo
Como fazer procedimentos no superlogo
 
Java presentation on insertion sort
Java presentation on insertion sortJava presentation on insertion sort
Java presentation on insertion sort
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
Graph.pptx
Graph.pptxGraph.pptx
Graph.pptx
 
Sorting Methods.pptx
Sorting Methods.pptxSorting Methods.pptx
Sorting Methods.pptx
 
Sorting
SortingSorting
Sorting
 
ADA_2_Analysis of Algorithms
ADA_2_Analysis of AlgorithmsADA_2_Analysis of Algorithms
ADA_2_Analysis of Algorithms
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
 
TUTE: Basic 0f HTML | ICT (GRADE 11 NEW) By:Raza Malhardeen©
TUTE: Basic 0f HTML | ICT (GRADE  11 NEW) By:Raza Malhardeen©TUTE: Basic 0f HTML | ICT (GRADE  11 NEW) By:Raza Malhardeen©
TUTE: Basic 0f HTML | ICT (GRADE 11 NEW) By:Raza Malhardeen©
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Algoritmos Gulosos - Troco Mínimo
Algoritmos Gulosos - Troco MínimoAlgoritmos Gulosos - Troco Mínimo
Algoritmos Gulosos - Troco Mínimo
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Aula 8 - Validação - parte 1
Aula 8 - Validação - parte 1Aula 8 - Validação - parte 1
Aula 8 - Validação - parte 1
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 

Similar to Algoritmo Counting sort

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear Time
Andres Mendez-Vazquez
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
kinan keshkeh
 
Top school in delhi ncr
Top school in delhi ncrTop school in delhi ncr
Top school in delhi ncr
Edhole.com
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdf
JavedAnsari236392
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)
mailund
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
웅식 전
 
Media pembelajaran matematika (operasi bilangan Bulat)
Media pembelajaran matematika (operasi bilangan Bulat)Media pembelajaran matematika (operasi bilangan Bulat)
Media pembelajaran matematika (operasi bilangan Bulat)
adekfatimah
 
Integers_Intro.ppt
Integers_Intro.pptIntegers_Intro.ppt
Integers_Intro.ppt
FREDERICKFORTUNA
 
include ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdfinclude ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdf
adisainternational
 
Logic development
Logic developmentLogic development
Logic development
Jerin John
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
Hossain Md Shakhawat
 
week4_python.docx
week4_python.docxweek4_python.docx
week4_python.docx
Radhe Syam
 
Arrays
ArraysArrays
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 
Algorithms lecture 3
Algorithms lecture 3Algorithms lecture 3
Algorithms lecture 3
Mimi Haque
 
2.1 order of operations w
2.1 order of operations w2.1 order of operations w
2.1 order of operations w
Tzenma
 
Sorting Technique
Sorting TechniqueSorting Technique
Sorting Technique
Salman Vadsarya
 

Similar to Algoritmo Counting sort (20)

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
06 Analysis of Algorithms: Sorting in Linear Time
06 Analysis of Algorithms:  Sorting in Linear Time06 Analysis of Algorithms:  Sorting in Linear Time
06 Analysis of Algorithms: Sorting in Linear Time
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
 
Top school in delhi ncr
Top school in delhi ncrTop school in delhi ncr
Top school in delhi ncr
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdf
 
Wu Mamber (String Algorithms 2007)
Wu  Mamber (String Algorithms 2007)Wu  Mamber (String Algorithms 2007)
Wu Mamber (String Algorithms 2007)
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
Media pembelajaran matematika (operasi bilangan Bulat)
Media pembelajaran matematika (operasi bilangan Bulat)Media pembelajaran matematika (operasi bilangan Bulat)
Media pembelajaran matematika (operasi bilangan Bulat)
 
Integers_Intro.ppt
Integers_Intro.pptIntegers_Intro.ppt
Integers_Intro.ppt
 
include ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdfinclude ltstdiohgt include ltstdlibhgt include .pdf
include ltstdiohgt include ltstdlibhgt include .pdf
 
Logic development
Logic developmentLogic development
Logic development
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
week4_python.docx
week4_python.docxweek4_python.docx
week4_python.docx
 
Arrays
ArraysArrays
Arrays
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Algorithms lecture 3
Algorithms lecture 3Algorithms lecture 3
Algorithms lecture 3
 
2.1 order of operations w
2.1 order of operations w2.1 order of operations w
2.1 order of operations w
 
Sorting Technique
Sorting TechniqueSorting Technique
Sorting Technique
 

More from Fernando Simeone

Computacao Numérica
Computacao NuméricaComputacao Numérica
Computacao Numérica
Fernando Simeone
 
Hierarquia de Chomsky
Hierarquia de ChomskyHierarquia de Chomsky
Hierarquia de Chomsky
Fernando Simeone
 
Algoritmos aproximativos
Algoritmos aproximativosAlgoritmos aproximativos
Algoritmos aproximativos
Fernando Simeone
 
Algoritmo SMA*
Algoritmo SMA*Algoritmo SMA*
Algoritmo SMA*
Fernando Simeone
 
Javascript
JavascriptJavascript
Javascript
Fernando Simeone
 
Guia da Startup
Guia da StartupGuia da Startup
Guia da Startup
Fernando Simeone
 

More from Fernando Simeone (6)

Computacao Numérica
Computacao NuméricaComputacao Numérica
Computacao Numérica
 
Hierarquia de Chomsky
Hierarquia de ChomskyHierarquia de Chomsky
Hierarquia de Chomsky
 
Algoritmos aproximativos
Algoritmos aproximativosAlgoritmos aproximativos
Algoritmos aproximativos
 
Algoritmo SMA*
Algoritmo SMA*Algoritmo SMA*
Algoritmo SMA*
 
Javascript
JavascriptJavascript
Javascript
 
Guia da Startup
Guia da StartupGuia da Startup
Guia da Startup
 

Recently uploaded

What is an RPA CoE? Session 2 – CoE Roles
What is an RPA CoE?  Session 2 – CoE RolesWhat is an RPA CoE?  Session 2 – CoE Roles
What is an RPA CoE? Session 2 – CoE Roles
DianaGray10
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
ScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking ReplicationScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking Replication
ScyllaDB
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!
Tobias Schneck
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
ScyllaDB
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptxAI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
Sunil Jagani
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
AlexanderRichford
 
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Ukraine
 
MySQL InnoDB Storage Engine: Deep Dive - Mydbops
MySQL InnoDB Storage Engine: Deep Dive - MydbopsMySQL InnoDB Storage Engine: Deep Dive - Mydbops
MySQL InnoDB Storage Engine: Deep Dive - Mydbops
Mydbops
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
manji sharman06
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's TipsGetting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
ScyllaDB
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 

Recently uploaded (20)

What is an RPA CoE? Session 2 – CoE Roles
What is an RPA CoE?  Session 2 – CoE RolesWhat is an RPA CoE?  Session 2 – CoE Roles
What is an RPA CoE? Session 2 – CoE Roles
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
ScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking ReplicationScyllaDB Tablets: Rethinking Replication
ScyllaDB Tablets: Rethinking Replication
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptxAI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
 
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
GlobalLogic Java Community Webinar #18 “How to Improve Web Application Perfor...
 
MySQL InnoDB Storage Engine: Deep Dive - Mydbops
MySQL InnoDB Storage Engine: Deep Dive - MydbopsMySQL InnoDB Storage Engine: Deep Dive - Mydbops
MySQL InnoDB Storage Engine: Deep Dive - Mydbops
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
Call Girls Chandigarh🔥7023059433🔥Agency Profile Escorts in Chandigarh Availab...
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's TipsGetting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
Getting the Most Out of ScyllaDB Monitoring: ShareChat's Tips
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 

Algoritmo Counting sort

  • 1. Counting Sort Fernando Simeone Mestrado em Ciência da Computação Universidade Federal de Lavras ! Projeto e Análise de Algoritmos (2014/2)
  • 2. Tópicos • O algoritmo • Análise de complexidade • Conclusão • Referências
  • 4. Idéia algoritmo Para cada elemento do array ! Contar quantos elementos há menores do que ele ! Inserir o elemento em sua posição correta
  • 6. Entradas Counting-Sort( input, output, k ) 2 5 3 0 2 3 0 3 input
  • 7. Entradas Counting-Sort( input, output, k ) 2 5 3 0 2 3 0 3 input inteiros maiores ou iguais a 0
  • 8. Entradas Counting-Sort( input, output, k ) 2 5 3 0 2 3 0 3 input output inteiros maiores ou iguais a 0
  • 9. Entradas Counting-Sort( input, output, k ) 2 5 3 0 2 3 0 3 input output k = 5 inteiros maiores ou iguais a 0
  • 10. Entradas Counting-Sort( input, output, k ) 2 5 3 0 2 3 0 3 input output k = 5 inteiros maiores ou iguais a 0
  • 11. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 output
  • 12. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 output 1 - Inicialização 0 1 2 3 4 5 6 7
  • 13. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 1 2 3 4 5 6 7
  • 14. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 1 2 3 4 5 6 7
  • 15. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 0 1 2 3 4 5 6 7
  • 16. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 0 0 1 2 3 4 5 6 7
  • 17. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 0 0 0 1 2 3 4 5 6 7
  • 18. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 0 0 0 0 1 2 3 4 5 6 7
  • 19. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 0 0 0 0 0 1 2 3 4 5 6 7
  • 20. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input k = 5 2 5 3 0 2 3 0 3 count 0 1 2 3 4 5 output 1 - Inicialização 0 0 0 0 0 0 0 1 2 3 4 5 6 7
  • 21. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 0 0
  • 22. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 0 0 j = 0
  • 23. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 0 0 j = 0 count[2] = count[2] + 1
  • 24. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 0 0 + 1 j = 0 1 count[2] = count[2] + 1
  • 25. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 1 0 j = 1
  • 26. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 1 0 j = 1 count[5] = count[5] + 1
  • 27. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 1 0 + 1 j = 1 1 count[5] = count[5] + 1
  • 28. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 1 1 j = 2
  • 29. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 1 1 j = 2 count[3] = count[3] + 1
  • 30. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 0 0 0 5 output 2 - Contagem A 1 1 + 1 j = 2 1 count[3] = count[3] + 1
  • 31. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 1 0 0 5 output 2 - Contagem A 1 1 j = 3
  • 32. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 1 0 0 5 output 2 - Contagem A 1 1 j = 3 count[0] = count[0] + 1
  • 33. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 0 0 1 0 0 5 output 2 - Contagem A 1 1 + 1 j = 3 1 count[0] = count[0] + 1
  • 34. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 1 0 0 5 output 2 - Contagem A 1 1 j = 4
  • 35. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 1 0 0 5 output 2 - Contagem A 1 1 j = 4 count[2] = count[2] + 1
  • 36. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 1 0 0 5 output 2 - Contagem A 1 1 + 1 j = 4 2 count[2] = count[2] + 1
  • 37. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 1 0 0 5 output 2 - Contagem A 2 1 j = 5
  • 38. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 1 0 0 5 output 2 - Contagem A 2 1 j = 5 count[3] = count[3] + 1
  • 39. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 1 0 0 5 output 2 - Contagem A 2 1 + 1 j = 5 2 count[3] = count[3] + 1
  • 40. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 2 0 0 5 output 2 - Contagem A 2 1 j = 6
  • 41. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 2 0 0 5 output 2 - Contagem A 2 1 j = 6 count[0] = count[0] + 1
  • 42. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 0 0 2 0 0 5 output 2 - Contagem A 2 1 + 1 j = 6 2 count[0] = count[0] + 1
  • 43. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 2 5 3 0 2 3 0 3 O algoritmo input count 0 1 2 3 4 2 0 0 2 0 0 5 output 2 - Contagem A 2 1 j = 7
  • 44. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 2 5 3 0 2 3 0 3 O algoritmo input count 0 1 2 3 4 2 0 0 2 0 0 5 output 2 - Contagem A 2 1 j = 7 count[3] = count[3] + 1
  • 45. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 2 5 3 0 2 3 0 3 O algoritmo input count 0 1 2 3 4 2 0 0 2 0 0 5 output 2 - Contagem A 2 1 + 1 j = 7 3 count[3] = count[3] + 1
  • 46. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 1 0 1 2 3 4 5 6 7
  • 47. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 1 + 0 1 2 3 4 5 6 7
  • 48. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 + 0 1 2 3 4 5 6 7
  • 49. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 + 0 1 2 3 4 5 6 7
  • 50. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 + 0 1 2 3 4 5 6 7
  • 51. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 + 0 1 2 3 4 5 6 7
  • 52. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 7 + 0 1 2 3 4 5 6 7
  • 53. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 7 + 0 1 2 3 4 5 6 7
  • 54. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 7 7 + 0 1 2 3 4 5 6 7
  • 55. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 7 7 + 0 1 2 3 4 5 6 7
  • 56. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 0 0 3 0 0 5 output 2 - Contagem B 2 12 4 7 7 8 0 1 2 3 4 5 6 7
  • 57. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 7 7 0 5 output 3 - Ordenação 4 8 0 1 2 3 4 5 6 7
  • 58. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 7 7 0 5 output 3 - Ordenação 4 8 j = 7 0 1 2 3 4 5 6 7
  • 59. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 7 7 0 5 output 3 - Ordenação 4 8 - 1 j = 7 0 1 2 3 4 5 6 7
  • 60. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 7 7 0 5 output 3 - Ordenação 4 8 - 1 j = 7 0 1 2 3 4 5 6 7 3
  • 61. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 7 7 0 5 output 3 - Ordenação 4 8 - 1 j = 7 0 1 2 3 4 5 6 7 6 3
  • 62. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 6 7 0 5 output 3 3 - Ordenação 4 8 j = 6 0 1 2 3 4 5 6 7
  • 63. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 6 7 0 5 output 3 3 - Ordenação 4 8 - 1 j = 6 0 1 2 3 4 5 6 7
  • 64. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 6 7 0 5 output 3 3 - Ordenação 4 8 - 1 j = 6 0 1 2 3 4 5 6 7 0
  • 65. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 2 2 0 6 7 0 5 output 3 3 - Ordenação 4 8 - 1 j = 6 0 1 2 3 4 5 6 7 1 0
  • 66. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 6 7 0 5 output 0 3 3 - Ordenação 4 8 j = 5 0 1 2 3 4 5 6 7
  • 67. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 6 7 0 5 output 0 3 3 - Ordenação 4 8 - 1 j = 5 0 1 2 3 4 5 6 7
  • 68. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 6 7 0 5 output 0 3 3 - Ordenação 4 8 - 1 j = 5 0 1 2 3 4 5 6 7 3
  • 69. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 6 7 0 5 output 0 3 3 - Ordenação 4 8 - 1 j = 5 0 1 2 3 4 5 6 7 5 3
  • 70. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 3 3 3 - Ordenação 4 8 j = 4 0 1 2 3 4 5 6 7
  • 71. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 3 3 3 - Ordenação 4 8 - 1 j = 4 0 1 2 3 4 5 6 7
  • 72. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 3 3 3 - Ordenação 4 8 - 1 j = 4 0 1 2 3 4 5 6 7 2
  • 73. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 3 3 3 - Ordenação 4 8 - 1 j = 4 0 1 2 3 4 5 6 7 3 2
  • 74. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 2 3 3 3 - Ordenação 3 8 j = 3 0 1 2 3 4 5 6 7
  • 75. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 2 3 3 3 - Ordenação 3 8 - 1 j = 3 0 1 2 3 4 5 6 7
  • 76. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 2 3 3 3 - Ordenação 3 8 - 1 j = 3 0 1 2 3 4 5 6 7 0
  • 77. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 1 2 0 5 7 0 5 output 0 2 3 3 3 - Ordenação 3 8 - 1 j = 3 0 1 2 3 4 5 6 7 0 0
  • 78. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 5 7 0 5 output 0 0 2 3 3 3 - Ordenação 3 8 j = 2 0 1 2 3 4 5 6 7
  • 79. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 5 7 0 5 output 0 0 2 3 3 3 - Ordenação 3 8 - 1 j = 2 0 1 2 3 4 5 6 7
  • 80. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 5 7 0 5 output 0 0 2 3 3 3 - Ordenação 3 8 - 1 j = 2 0 1 2 3 4 5 6 7 3
  • 81. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 5 7 0 5 output 0 0 2 3 3 3 - Ordenação 3 8 - 1 j = 2 0 1 2 3 4 5 6 7 4 3
  • 82. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 3 - Ordenação 3 8 j = 1 0 1 2 3 4 5 6 7
  • 83. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 3 - Ordenação 3 8 - 1 j = 1 0 1 2 3 4 5 6 7
  • 84. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 3 - Ordenação 3 8 - 1 j = 1 0 1 2 3 4 5 6 7 5
  • 85. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 3 - Ordenação 3 8 - 1 j = 1 0 1 2 3 4 5 6 7 7 5
  • 86. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 5 3 - Ordenação 3 7 j = 0 0 1 2 3 4 5 6 7
  • 87. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 5 3 - Ordenação 3 7 - 1 j = 0 0 1 2 3 4 5 6 7
  • 88. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 5 3 - Ordenação 3 7 - 1 j = 0 0 1 2 3 4 5 6 7 2
  • 89. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 3 3 3 5 3 - Ordenação 3 7 - 1 j = 0 0 1 2 3 4 5 6 7 2 2
  • 90. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 0 1 2 3 4 5 6 7 O algoritmo input 2 5 3 0 2 3 0 3 count 0 1 2 3 4 0 2 0 4 7 0 5 output 0 0 2 2 3 3 3 5 3 - Ordenação 2 7 0 1 2 3 4 5 6 7
  • 92. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5
  • 93. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5
  • 94. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5
  • 95. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5
  • 96. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1
  • 97. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1 n
  • 98. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1 n k + 1
  • 99. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1 n k + 1 k
  • 100. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1 n k + 1 k n + 1
  • 101. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1 n k + 1 k n + 1 n
  • 102. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input1 - Pior caso 1 k + 2 k + 1 input count Output 0 1 2 3 4 5 6 7 5 3 3 3 2 2 0 0 0 1 2 3 4 5 6 7 0 1 2 3 4 5 n + 1 n k + 1 k n + 1 n n
  • 104. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) 1 - Pior caso
  • 105. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 1 - Pior caso
  • 106. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = an + b 1 - Pior caso
  • 107. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = an + b a = c4 + c5 +c8 + c9 +c10 1 - Pior caso
  • 108. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = an + b a = c4 + c5 +c8 + c9 +c10 b = k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 1 - Pior caso
  • 109. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = an + b a = c4 + c5 +c8 + c9 +c10 b = k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = O(n) + O(k) 1 - Pior caso
  • 110. Análise de complexidade c1 + c2(k+2) + c3(k+1) + c4(n+1) + c5(n) + c6(k+1) + c7(k) + c8(n + 1) + c9(n)+ c10(n) n (c4 + c5 +c8 + c9 +c10) + k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = an + b a = c4 + c5 +c8 + c9 +c10 b = k (c2 + c3 + c6 +c7) + 2c2 + c3 + c4 +c6 + c8 T(n) = O(n) + O(k) 1 - Pior caso = O(n + k)
  • 111. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input2 - Melhor caso input 0 0 2 2 3 3 3 5 0 1 2 3 4 5 6 7
  • 112. Counting-sort (input, output, k) ! 1 count = new int [ k ] ! 2 for i = 0 to k 3 count[ i ] = 0 ! 4 for j = 0 to input.length - 1 5 count[ input[ j ] ] = count[ input[ j ] ] + 1 ! 6 for i = 1 to k 7 count[ i ] = count[ i ] + count[ i - 1 ] ! 8 for j = A.length - 1 downto 0 9 output[ count[ input[ j ] ] -1 ] = input[ j ] 10 count[ input[ j ] ] = count[ input[ j ] ] - 1 Análise de complexidade input2 - Melhor caso input 0 0 2 2 3 3 3 5 0 1 2 3 4 5 6 7 O número de operações! não depende da! ordem inicial do array
  • 115. Análise de complexidade T(n) = 𝛩(n + k) Isso faz com que o counting sort seja uma boa opção quando k ≤ n" pois assim seu custo será 𝛩(n).
  • 118. Conclusão • Counting Sort é um algoritmo de complexidade linear;
  • 119. Conclusão • Counting Sort é um algoritmo de complexidade linear; • Não respeita o limite inferior 𝝮 (n lg n) aplicado a algoritmos baseados em comparações;
  • 120. Conclusão • Counting Sort é um algoritmo de complexidade linear; • Não respeita o limite inferior 𝝮 (n lg n) aplicado a algoritmos baseados em comparações; • Possui complexidade O(n) quando K ≤ n;
  • 121. Conclusão • Counting Sort é um algoritmo de complexidade linear; • Não respeita o limite inferior 𝝮 (n lg n) aplicado a algoritmos baseados em comparações; • Possui complexidade O(n) quando K ≤ n; • É um algoritmo estável, valores iguais permanecem na mesma ordem entre si.
  • 123. Referências CORMEN, T. H.; LEISERSON, C. E.; RIVEST, R. L.; STEIN, C. Introduction to Algorithms, Third Edition. 3rd. ed. [S.l.]: The MIT Press, 2009. ISBN 0262033844, 9780262033848. ! Gonzaga de Oliveira, S.L. . Algoritmos e seus fundamentos. 1. ed. Lavras: Editora UFLA, 2011. v. 300. 440p .