Projeto e Anlise de Algoritmos Prof Ruy Luiz

  • Slides: 12
Download presentation
Projeto e Análise de Algoritmos Prof. Ruy Luiz Milidiú 10/6/2020 Ruy Luiz Milidiú 1

Projeto e Análise de Algoritmos Prof. Ruy Luiz Milidiú 10/6/2020 Ruy Luiz Milidiú 1

Quicksort z 1960 Hoare z. Dividir-e-conquistar z. Idéia: Partição z. In loco z. Simples

Quicksort z 1960 Hoare z. Dividir-e-conquistar z. Idéia: Partição z. In loco z. Simples z. Caso médio 10/6/2020 Ruy Luiz Milidiú 2

Idéia: partição. . . PEQUENOS P I V O T GRANDES z. Posicionar pivot

Idéia: partição. . . PEQUENOS P I V O T GRANDES z. Posicionar pivot z. Separar os pequenos dos grandes 10/6/2020 Ruy Luiz Milidiú 3

Partição PEQUENOS GRANDES Pivot posicionado ! 10/6/2020 Ruy Luiz Milidiú 4

Partição PEQUENOS GRANDES Pivot posicionado ! 10/6/2020 Ruy Luiz Milidiú 4

Partição if esq < dir then pivot = a[dir]; peq = esq-1; grd =

Partição if esq < dir then pivot = a[dir]; peq = esq-1; grd = dir; repeat peq ++ until a[] >= pivot; repeat grd -- until a[] <= pivot; t=a[peq]; a[peq]=a[grd]; a[grd]=t; until grd < peq; a[grd]=a[peq]; a[peq]=pivot; a[dir]=t; 10/6/2020 Ruy Luiz Milidiú 5

Quicksort < AQUI VAI UMA ANIMAÇÃO DO QUICKSORT > 10/6/2020 Ruy Luiz Milidiú 6

Quicksort < AQUI VAI UMA ANIMAÇÃO DO QUICKSORT > 10/6/2020 Ruy Luiz Milidiú 6

Quicksort quicksort(esq, dir) if esq < dir then pivot = a[dir]; peq = esq-1;

Quicksort quicksort(esq, dir) if esq < dir then pivot = a[dir]; peq = esq-1; grd = dir; repeat peq++ until a[peq] >= pivot; repeat grd-- until a[grd] <= pivot; t=a[peq]; a[peq]=a[grd]; a[grd]=t; until grd < peq; a[grd]=a[peq]; a[peq]=pivot; a[dir]=t; quicksort(esq, peq); quicksort(grd+1, dir); 10/6/2020 Ruy Luiz Milidiú 7

Sentinelas MIN 10/6/2020 Ruy Luiz Milidiú 8

Sentinelas MIN 10/6/2020 Ruy Luiz Milidiú 8

Melhor caso PEQUENOS P I V O T GRANDES z. Partições balanceadas T(n) =

Melhor caso PEQUENOS P I V O T GRANDES z. Partições balanceadas T(n) = 2. T(n/2) + n 10/6/2020 Ruy Luiz Milidiú 9

Pior caso P I V O T GRANDES z. Partições totalmente desbalanceadas T(n) =

Pior caso P I V O T GRANDES z. Partições totalmente desbalanceadas T(n) = T(n-1) + n 10/6/2020 Ruy Luiz Milidiú 10

Caso médio T(n) = = T(0) T(1) T(2) T(3) + + T(n-1) T(n-2) T(n-3)

Caso médio T(n) = = T(0) T(1) T(2) T(3) + + T(n-1) T(n-2) T(n-3) T(n-1) + + n+1 n+1 . . . T(n) = T(n-3) + T(2) + n+1 T(n) = T(n-2) + T(1) T(n) = T(n-1) + T(0) + n+1 M(n) = n+1 10/6/2020 + (2/n). [M(0) + … + M(n-1)] Ruy Luiz Milidiú 11

Caso médio M(n) = n+1 n. M(n) = n. (n+1) + + (2/n). [M(0)

Caso médio M(n) = n+1 n. M(n) = n. (n+1) + + (2/n). [M(0) + … + M(n-1)] 2. [M(0) + … + M(n-1)] n. M(n) = n. (n+1) + [(n-1). M(n-1) - (n-1). n] + 2. M(n-1) M(n)/(n+1) = M(n-1)/n + 2/(n+1) M(n)/(n+1) = 2/(n+1) + 2/n + … + 2/5 + 2/4 + M(2)/3 n+1 M(n)/(n+1) 2. (1/j) 2. 1 n+1 (1/x) = 2 ln(n+1) J=2 M(n) = (n+1). ln(n+1) 10/6/2020 Ruy Luiz Milidiú 12