Baixe o app para aproveitar ainda mais
Prévia do material em texto
Ordenação por Inserção ( Insertion Sort ) #include <stdio.h> int main () { int vetor[5] = {9, 8, 7, 6, 5}; int i, j, aux; for(i = 1; i < 5; i++) { aux = vetor[i]; for(j = i - 1; (j >= 0) && (aux < vetor[j]); j--) vetor[j + 1] = vetor[j]; vetor[j + 1] = aux; } return 0; } Preencher: i j aux vetor[j + 1] = vetor[j]; vetor[j + 1] = aux; 1 0 8 {9 , 9 , 7, 6, 5} 1 -1 8 {8 , 9 , 7, 6, 5} 2 1 7 {8 , 9, 9 , 6, 5} 2 0 7 {8, 8 , 9 , 6, 5} 2 -1 7 {7, 8, 9, 6, 5}
Compartilhar