EstruturasHomogeneas-Vetores03 Bubble Sort

Preview:

Citation preview

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

Um dos algoritmos de ordenação mais simples é o Bubble Sort, mais conhecida como "Algoritmo da Bolha”. A ideia é ir comparando elementos dois a dois e trocá-los de ordem, dependendo de como se deseja ordenar o vetor (crescente ou descrescente). Após isso ser feito n vezes, o vetor final estará ordenado.

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

9 8 4 6 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

9 8 4 6 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 9 4 6 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 9 4 6 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 9 6 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 9 6 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 6 9 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 6 9 3

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 6 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 6 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

8 4 6 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 8 6 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 8 6 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 6 8 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 6 8 3 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 6 3 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 6 3 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 6 3 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 6 3 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 3 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 3 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 3 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

4 3 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

3 4 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

3 4 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

3 4 6 8 9

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

8 4 6 3 94 6 3 8 9

4 3 6 8 93 4 6 8 9

9 8 4 6 3

4 comparações

3 comparações

2 comparações

1 comparações

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Ordenação bubble sort

Comparações = n * (n-1)/2 5 elementos = 5 * ( 5-1)/2 = 1010 elementos = 10 * (10-1)/2 = 45

Prof. Aparecido Vilela JuniorProf. Alexandre Moreno

Estrutura de dados homogêneasOrdenação bubble sort (bolha)

Algoritmo para classificação de 5 elementos:

para x de 1 ate tam-1 faca para y de 1 ate tam-x faca se v[y] > v[y+1] entao tmp:=v[y] v[y]:=v[y+1] v[y+1]:=tmp fimse fimpara fimpara

Recommended