1 / 9

Algoritmos de Ordenação

Algoritmos de Ordenação. CONTEÚDO (1) Auto-avaliação (2) InsertionSort (3) SelectionSort. Ordenação/Classificação. Antes de prosseguir:

draco
Télécharger la présentation

Algoritmos de Ordenação

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algoritmos de Ordenação CONTEÚDO (1) Auto-avaliação (2) InsertionSort (3) SelectionSort

  2. Ordenação/Classificação Antes de prosseguir: Transforme o algoritmo bubbleSortRecursivo da aula passada, de modo que ele continue sendo recursivo, mas que não use nenhuma estrutura de repetição, como por exemplo for, while, ou qualquer outra. (1)Auto-avaliação

  3. Ordenação por Inserção (Insertion Sort) Baseado na técnica usada por jogadores de cartas Quando um jogador pega uma nova carta, ele abre espaço para a nova carta e então insere-a no seu lugar apropriado O jogador mantém em ordem as cartas já pegas (2)InsertionSort

  4. Ordenação por Inserção (Insertion Sort) (2)InsertionSort 7 4 1 9 2 i=1 7 7 1 9 2 1 4 7 2 9 4 7 1 9 2 1 4 7 9 9 4 7 1 9 2 1 4 7 9 7 i=4 4 7 7 9 2 1 4 4 9 7 i=2 4 4 7 2 9 1 2 4 9 7 1 4 7 2 9 i=3 1 4 7 9 2

  5. Pseudocódigo Algorithm Insertion_Sort (A, N): Input: An array A storing N items Output: A sorted in ascending order for i  1 to N-1 do { current  A[i] j  i while (A[j-1] > current) And (j>0) A[j]  A[j-1] j  j-1 A[j]  current } (2)InsertionSort O(n2)

  6. #include <stdio.h> void insertionSort(int n, int v[]){ int i, j, elemento; for (i=0; i<n;i++){ elemento=v[i]; j=i-1; while (j>=0 && elemento<v[j]){ v[j+1]=v[j]; j--; } v[j+1] = elemento; } } void imprimeVetor(int n, int v[]){ int i; for (i=0;i<n;i++) printf(" %3d",v[i]); } main(){ int qtde = 5; int vetor[10] = {2,6,1,3,7,5,4,9,10,8}; imprimeVetor(qtde, vetor); insertionSort(qtde, vetor); printf("\n"); imprimeVetor(qtde, vetor); getchar(); } (2)InsertionSort Código em C

  7. Ordenação por Seleção (Selection Sort) Encontre o menor elemento no array e troque-o com o elemento na primeira posição. Encontre o segundo menor elemento no array e troque-o com o elemento na segunda posição, e assim por diante até que o array todo esteja ordenado. (3)SelectionSort

  8. Pseudocódigo Algorithm Selection_Sort (A, N): Input: An array A storing N items Output: A sorted in ascending order for i  0 to N-2 do min  i for j  i+1 to N-1 do if A[j] < A[min] then min  j temp  A[min] A[min]  A[i] A[i]  temp (3)SelectionSort O(n2)

  9. #include <stdio.h> void troca(int i, int j, int v[]){ int aux= v[i]; v[i]=v[j]; v[j]=aux; } int encontraIndiceDoMenor(int inicio, int fim, int v[]){ int i, indiceDoMenor=inicio; for (i=inicio; i<=fim;i++){ if (v[i]<v[indiceDoMenor]) indiceDoMenor=i; } return indiceDoMenor; } void selectionSort(int n, int v[]){ int i; for (i=0;i<n;i++){ troca(i,encontraIndiceDoMenor(i,n-1,v), v); } } main(){ int qtde = 5; int vetor[10] = {2,6,1,3,7,5,4,9,10,8}; imprimeVetor(qtde, vetor); selectionSort(qtde, vetor); printf("\n"); imprimeVetor(qtde, vetor); getchar(); } (3)SelectionSort Código em C

More Related