Enviado em 29/05/2018 - 01:26h
"Boa meia noite"! Estou estudando por conta própria algoritmos de ordenação de vetores e eu estava olhando mais sobre
selection sort e inserction sort, mas eu não consegui entender a diferença entre esses dois "escargots" e acabei escrevendo o seguinte código:
De forma expandida:
Agora eu gostaria de saber se as gambiarras acima são do tipo selection sort ou inserction sort?
selection sort e inserction sort, mas eu não consegui entender a diferença entre esses dois "escargots" e acabei escrevendo o seguinte código:
///////
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int main(void){
int vet[SIZE]={45, 20, 89, 60, 35, 78, 13, 25, 97, 61};
printf("\n\nDesordenado: ");
for(unsigned int i=0; i<SIZE; i++){
printf("[%d] ", vet[i]);
}
printf("\n\n");
int swap;
for(unsigned int x=0; x<SIZE; x++){
for(unsigned int y=0; y<SIZE; y++){
if(vet[x]<vet[y]){
swap=vet[x];
vet[x]=vet[y];
vet[y]=swap;
}
}
}
printf("\n\nOrdenado: ");
for(unsigned int i=0; i<SIZE; i++){
printf("[%d] ", vet[i]);
}
printf("\n\n");
system("pause"); //troll detected...
return 0;
}
De forma expandida:
Veja funcionando no seguinte link -> cpp.sh/9f6qa
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int main(void){
int vet[SIZE]={45, 20, 89, 60, 35, 78, 13, 25, 97, 61};
printf("\n\nDesordenado: ");
for(unsigned int i=0; i<SIZE; i++){
printf("[%d] ", vet[i]);
}
printf("\n\n\n\tOrdenando: \n\n\n");
int swap;
for(unsigned int x=0; x<SIZE; x++){
for(unsigned int y=0; y<SIZE; y++){
printf("[%d]=%d eh menor que [%d]=%d??? ", x, vet[x], y, vet[y]);
if(vet[x]<vet[y]){
printf("Yes!\n\n");
swap=vet[x];
vet[x]=vet[y];
vet[y]=swap;
for(unsigned int i=0; i<SIZE; i++){
printf("[%d] ", vet[i]);
}
printf("\n\n");
}else{
printf("No!\n");
}
}
}
printf("\n\nOrdenado: ");
for(unsigned int i=0; i<SIZE; i++){
printf("[%d] ", vet[i]);
}
printf("\n\n");
return 0;
}
Agora eu gostaria de saber se as gambiarras acima são do tipo selection sort ou inserction sort?