ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460.

32
ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho 7547722 Vinícius Bertaco Neves 7127460

Transcript of ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460.

Page 1: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Quicksort

Page 27: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460.

Quicksort

Page 28: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460.

Quicksort

Page 29: ALGORITMOS DE ORDENAÇÃO Nayara Gatto Pracucho7547722 Vinícius Bertaco Neves7127460.

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.

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.

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.