Ordenar vetor com algoritmo Insertion Sort
Publicado por Santiago Staviski (última atualização em 22/06/2010)
[ Hits: 54.142 ]
Homepage: http://allenkonstanz.blogspot.com
Ordena um vetor em ordem crescente ou decrescente usando um método de ordenação Insertion Sort.
/*Autor: Allen Konstanz
* Web: allenkonstanz.blogspot.com */
#include <stdio.h>
void insertionSortD(int array[], int tamanho) {
int i, j, tmp;
for (i = 1; i < tamanho; i++) {
j = i;
while (j > 0 && array[j - 1] < array[j]) {
tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
j--;
}
}
}
void insertionSortC(int array[], int tamanho) {
int i, j, tmp;
for (i = 1; i < tamanho; i++) {
j = i;
while (j > 0 && array[j - 1] > array[j]) {
tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
j--;
}
}
}
int main(int argc, char** argv)
{
int array[100], tamanho, ordem;
printf("\n\n\t Entre com o número de termos...: ");
scanf("%d", &tamanho);
printf("\n\t Entre com os elementos do array...:");
for(int i = 0; i < tamanho;i++){
printf("\nDigite o %dº número: ",i+1);
scanf("%d",&array[i]);
}
printf("\n\t Digite 1 para ordernar o array em ordem crescente ou 2 para ordem decrescente: ");
scanf("%d",&ordem);
if (ordem == 1){
insertionSortC(array,tamanho);
printf("\nArray em ordem crescente: ");
for(int i=0; i<tamanho; i++){
printf("\n\t\t\t%d", array[i]);
}
}else if (ordem ==2) {
insertionSortD(array,tamanho);
printf("\nArray em ordem decrescente: ");
for(int i=0; i<tamanho; i++){
printf("\n\t\t\t%d", array[i]);
}
}
return 0;
}
Realizando exponenciação interativa
NOÇÕES DE ARITMÉTICA DE PONTEIROS
aritmed1.c - Aritmetica de Ponteiros
Uma pequena "CALCULADORA" (apenas SOMA).
Nenhum comentário foi encontrado.
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Usando dracut e dispensando genkernel no Gentoo + LUKS + Btrfs
Curso GRÁTIS: OCS Inventory NG - Do Deploy ao Hardening com foco em Segurança da Informação!
Instalando fontes via script no Nautilus Scripts no Gnome do Debian 13
O que houve com slackware ??? (1)
Instalar Linux em notebook Sony Vaio VPCEG13EB (7)
VirtualBox no Ubuntu 25.10 sem redimensionam... automatico. (2)









