algoritmos de ordenaÇÃo nayara gatto pracucho7547722 vinícius bertaco neves7127460
TRANSCRIPT
![Page 1: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/1.jpg)
ALGORITMOS DE ORDENAÇÃO
Nayara Gatto Pracucho 7547722
Vinícius Bertaco Neves 7127460
![Page 2: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/2.jpg)
Bubble Sort
• A ideia é percorrer o vetor diversas vezes, fazendo “flutuar” para o topo o menor elemento da sequência.
• Se o vetor for considerado do tipo coluna, os elementos podem ser comparados com bolhas em um tanque de água, com densidades proporcionais ao valor das respectivas chaves.
![Page 3: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/3.jpg)
Bubble Sort
...
para i 2 até i N, com passo i i+1
para j N até j i, com passo j j-1
se ( a [ j - 1 ] > a [ j ] )
x a [ j – 1 ]
a [ j – 1 ] a [ j ]
a [ j ] x
fim se
fim para
fim para
...
![Page 4: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/4.jpg)
Bubble Sort
N = 8 N = 8 N = 8
45
56
12
43
95
19
8
67 j
j - 1
a
45
56
12
43
95
19
8
67
j
j - 1
a
45
56
12
43
95
8
19
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
![Page 5: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/5.jpg)
Bubble Sort
N = 8 N = 8 N = 8
45
56
12
43
95
8
19
67
j
j - 1
a
45
56
12
43
8
95
19
67
j
j - 1
a
45
56
12
43
8
95
19
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
![Page 6: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/6.jpg)
Bubble Sort
N = 8 N = 8 N = 8
45
56
12
8
43
95
19
67
j
j - 1
a
45
56
12
8
43
95
19
67
j
j - 1
a
45
56
8
12
43
95
19
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
![Page 7: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/7.jpg)
Bubble Sort
N = 8 N = 8 N = 8
45
56
8
12
43
95
19
67
j
j - 1
a
45
8
56
12
43
95
19
67
j
j - 1
a
45
8
56
12
43
95
19
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
![Page 8: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/8.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
45
56
12
43
95
19
67
j
j - 1
a
8
45
56
12
43
95
19
67
a
8
45
56
12
43
95
19
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
Término da primeira passagem (i = 2)
![Page 9: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/9.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
45
56
12
43
95
19
67
j
j - 1
a
8
45
56
12
43
19
95
67
a
8
45
56
12
43
19
95
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
![Page 10: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/10.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
45
56
12
19
43
95
67
j
j - 1
a
8
45
56
12
19
43
95
67
a
8
45
56
12
19
43
95
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
![Page 11: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/11.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
45
12
56
19
43
95
67
j
j - 1
a
8
45
12
56
19
43
95
67
a
8
12
45
56
19
43
95
67
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
ij
j - 1
![Page 12: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/12.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
45
56
19
43
95
67
a
8
12
45
56
19
43
95
67
a
8
12
45
56
19
43
67
95
a
j
j - 1
8
7
6
5
4
3
2
1
i
i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
Término da segunda passagem (i = 3)
![Page 13: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/13.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
45
56
19
43
67
95
a
8
12
45
56
19
43
67
95
a
8
12
45
56
19
43
67
95
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
j
j - 1
![Page 14: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/14.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
45
19
56
43
67
95
a
8
12
45
19
56
43
67
95
a
8
12
19
45
56
43
67
95
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
ij
j - 1
j
j - 1
![Page 15: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/15.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
19
45
56
43
67
95
a
8
12
45
19
56
43
67
95
a
8
12
19
45
56
43
67
95
a
j
j - 1
8
7
6
5
4
3
2
1
i
i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
Término da passagem (i = 4)
![Page 16: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/16.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
19
45
56
43
67
95
a
8
12
19
45
43
56
67
95
a
8
12
19
45
43
56
67
95
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
j
j - 1
![Page 17: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/17.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
j
j - 1
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1
Término da passagem (i = 5)
![Page 18: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/18.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1 j
j - 1
Término da passagem (i = 6)
![Page 19: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/19.jpg)
Bubble Sort
N = 8 N = 8 N = 8
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
i
j
j - 1 j
j - 1
Término da passagem (i = 7)
![Page 20: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/20.jpg)
Bubble Sort
N = 8 N = 8
8
12
19
43
45
56
67
95
a
8
12
19
43
45
56
67
95
a
8
7
6
5
4
3
2
1
i i
1
2
3
4
5
6
7
8j
j - 1
Término da passagem (i = 8)
![Page 21: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/21.jpg)
Bubble Sort• Complexidade:
• No melhor caso, o algoritmo executa n operações relevantes.• No pior caso, são feitas n² operações.
• Portanto, a complexidade desse algoritmo é de Ordem quadrática.
O(N²)
![Page 22: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/22.jpg)
Bubble Sort• Conclusão:
• O Bubble Sort é um método de simples implementação, porém a sua eficiência é menor entre os métodos de ordenação interna.
Vantagens Desvantagem
- Fácil implementação - Complexidade quadrática
- Algoritmo estável
![Page 23: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/23.jpg)
Quicksort• História:
• Método de ordenação muito rápido e eficiente, inventado por C.A.R. Hoare em 1960• Criou o Quicksort ao tentar traduzir um dicionário de inglês para
russo, ordenando as palavras. Tendo como objetivo reduzir o problema original em subproblemas que possam ser resolvidos mais fácil e rapidamente.
![Page 24: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/24.jpg)
Quicksort• Qual a ideia básica do Quicksort?
• A ideia básica é dividir o problema de ordenar um conjunto com n itens em dois subproblemas menores (estratégia de divisão e conquista).
• Os problemas menores são ordenados independentes.• Utiliza um elemento arbitrário chamado pivô.
• Geralmente é o elemento do meio• O pivô pode influenciar no desempenho
![Page 25: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/25.jpg)
Quicksort• Funcionamento detalhado: Algoritmo de Partição
• O vetor v é rearranjado por meio da escolha arbitrária de um pivô p
• O vetor v é particionado em dois:• Restarão dois ‘sub-vetores’ para serem ordenados.• Espera-se que o ‘sub-vetor’ esquerdo contenha elementos
menores que o pivô e o ‘sub-vetor’ direito, elementos maiores.
• O vetor é então percorrido em ambos os sentidos, e quando a condição não é satisfeita, os elementos são trocados.
• Termina cada ‘etapa’ quando ponteiros se cruzam, continua recursivamente.
![Page 26: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/26.jpg)
Quicksort
![Page 27: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/27.jpg)
Quicksort
![Page 28: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/28.jpg)
Quicksort
![Page 29: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/29.jpg)
Quicksort• Complexidade:
• Pior caso• Acontece quando o pivô é sempre o maior ou menor elemento (partições de
tamanho desequilibrado)• = O(n²)
• espaço/memória necessário no pior caso é linear•Melhor caso
•Acontece quando as partições têm sempre o mesmo tamanho (partições balanceadas).
•Pivô representa elemento mediano do conjunto.•C(n) = n(logn) = O (n logn)
•Caso médio•C(n) ~ 1,39 n logn = O (n logn)
![Page 30: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/30.jpg)
Quicksort• Análise:• Vantagens:
• Melhor opção para ordenar vetores grandes.• Muito rápido devido ao laço interno ser simples.
• Algoritmo Instável• Processo de partição não é estável
• Qualquer chave pode ser movida para trás de várias outras chaves iguais a si (que ainda não foram examinadas)
• Não é conhecida nenhuma forma simples de implementar uma versão estável.
• Não se deve chamar recursivamente se o vetor tiver tamanho 1
• Desvantagem:• Pior caso (n²)
![Page 31: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/31.jpg)
Quicksort• Otimizações: Outras abordagens
• Pivô baseado em uma Mediana• Aumentar o número de elementos considerados na mediana.
• Implementação não recursiva• Pilha auxiliar, que pode ter tamanho N.• Ordenar a partição menor primeiro.
• Algoritmo genérico incluído na biblioteca padrão• Stdlib.h• “ void qsort (void *v, int n, int tam, int (*cmp)(const void*, const void*)); ”
• Muito rápido devido ao laço interno ser simples.
![Page 32: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460](https://reader036.vdocuments.pub/reader036/viewer/2022062404/552fc12e497959413d8d3984/html5/thumbnails/32.jpg)
Biblioteca• http://dcm.ffclrp.usp.br/~augusto/icii/
• http://www.cos.ufrj.br/~rfarias/cos121/aula_05.html
• http://pt.wikipedia.org/wiki/Bubble_sort