0
//Questão 4
#include <stdio.h>
#define TAM 10
int main()
{
int i, valorVET, VET[TAM];
printf("Digite os 10 numeros:\n");
for(i = 0; i < TAM; i++)
{
scanf("%d", &VET[i]);
}
for(i = TAM - 1; i >= 0; i--)
{
if(i != 0)
{
if(VET[i] <= VET[i - 1])
{
valorVET = VET[i];
VET[i] = VET[i - 1];
VET[i - 1] = valorVET;
}
}
}
for(i = 0; i < TAM; i++)
{
if(i != 0)
{
if(VET[i] > VET[i + 1])
{
valorVET = VET[i];
VET[i] = VET[i + 1];
VET[i + 1] = valorVET;
}
}
}
printf("\n");
for(i = 0; i < TAM; i++)
{
printf("%d ", VET[i]);
}
printf("\n\n");
return 0;
}
Make a mistake ? Don’t order as you want ?
– Isac
No mistake, just not ordering in the ascending order
– Lucas Bandeira
To do the ordering you need a
for
within another, that is to sayfors
nested, this is following simple sorting algorithms like the Selection Sort or Bubble Sort– Isac
And how would that look? Can you tell me?
– Lucas Bandeira
I would put the other for comparison within the first or do another for logic different?
– Lucas Bandeira
I suggest you take the principle and follow the idea of the algorithm, getting a good idea of how it works, and then move on to the implementation. There is for example here on wikipedia references to the Bubble Sort, which is the most similar to what I was doing in the code. It also has other different sorting algorithms with their particularities
– Isac
All right, thank you very much!!
– Lucas Bandeira