Sorting algorithm does not work

Asked

Viewed 118 times

3

Something is missing in the program. It checks the order with a single number or a single time.

#include <stdio.h>
#include <stdlib.h>

  /*4)escrever 10 números e ordenar em forma crescente e decrescente*/

int main()
{
    int opc;
    int numA=0, numB=0, numC=0, soma, aux=0, i;
    int vet[10];
    float media;

    printf("escolha qual exercicio quer executar:\n1)Ex1\n2)Ex2\n3)Ex3\n4)Ex4\n");
    scanf("%i", &opc);

    fflush(stdin);
    system("cls");

    switch(opc)
    {
    case 4:

        printf("digite 10 numeros aleatorios:\n");

        /*inicio da fase de processamento*/
        for(i=0; i<10; i++)
        {
            scanf("%i", &vet[i]);
        }


    for(i=0; i<10; i++)/*crescente*/
    {
        if(vet[i]>vet[i+1])
        {
            aux=vet[i];
            vet[i]=vet[i+1];
            vet[i+1]=aux;
        }

        printf("%i", vet[i]);
    }

        /*fim da fase de processamento*/

        break;

    default:
        break;
    }

    return 0;
}
  • 4

    His algorithm is wrong, as he only compares one number to the next (and he would need to compare it to all next to the end, considering that it is already ordered up to it). Reading tip: http://pt.wikipedia.org/wiki/Bubble_sort

  • I’m sure this one I wrote right at the end of the first stretch my dear.

  • 3

    I don’t understand what you mean, my dear. :)

1 answer

4


The way you’re doing is just exchanging between two elements, but you don’t keep making the exchange between all the elements. There are several ways to solve this. To compare all the elements in a simple way it is necessary to make two loops to compare each element with all the others.

#include <stdio.h>

int main(void) {
    int vet[10];
    int aux;
    for (int i = 0; i < 10; i++) scanf("%i", &vet[i]);
    for (int i = 0; i < 10; i++) {
        for (int j = i + 1; j < 10; j++) { 
            if(vet[i] > vet[j]) {
                aux = vet[i];
                vet[i] = vet[j];
                vet[j] = aux;
            }
        }
        printf("%i", vet[i]);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • That’s right, it’s the Boole method (something like that) right? I did it but I did it with the 'j=0' and not 'j=i+1', thank you very much. = ) you always help me XD

  • 2

    j = i + 1 is the most efficient way because every step you can go comparing one less element since it is already certain that it is ordered. There are even more efficient ways but they’re more complicated, I don’t think that’s your goal.

  • lol XD is I noticed that @bigown, saw are 12 exercises if I have more doubt I can edit the question and you edit the answer XD

  • 2

    Don’t do this, if you have another problem, post another question.

  • always tell me this but all right, being q also my doubt can be doubt of other =) but thank you =) @bigown

Browser other questions tagged

You are not signed in. Login or sign up in order to post.