Baixe o app para aproveitar ainda mais
Prévia do material em texto
ESTRUTURA DE DADOS I ATIVIDADES DA AULA 05 Faça um programa na linguagem C/C++ que implemente o algoritmo de ordenação Insertion-Sort. O programa deverá permitir que o usuário informe n valores inteiros e visualize os valores ordenados em ordem crescente na tela. #include <iostream> using namespace std; // Aluno: Jander Nogueira void exibe_vetor(int *vetor, int qtd); void ordena_insertion(int *vetor, int qtd); int main(int argc, char **argv) { int qtd; std::cout << "Digite a quantidade de elementos: \n" << std::endl; std::cin >> qtd; int *vetor = new int[qtd]; for (int i = 0; i < qtd; i++) { std::cout << "Informe o " << i + 1 << "°" << " elemento: \n" << std::endl; std::cin >> vetor[i]; std::cout << std::endl; } std::cout << "Valores informados: \n"; std::cout << std::endl; exibe_vetor(vetor, qtd); ordena_insertion(vetor, qtd); delete[] vetor; return 0; } void exibe_vetor(int *vetor, int qtd) { for (int i = 0; i < qtd; i++) { std::cout << "|" << vetor[i] << "|\n" << std::endl; } } void ordena_insertion(int *vetor, int qtd) { int j, atual; for (int i = 1; i < qtd; i++) { atual = vetor[i]; j = i - 1; while ((j >= 0) && (atual < vetor[j])) { vetor[j + 1] = vetor[j]; j--; } vetor[j + 1] = atual; } std::cout <<"\nValores ordenados em ordem crescente: \n"; std::cout <<std::endl; exibe_vetor( vetor,qtd); }
Compartilhar