Application of Bubble Sort

Asked

Viewed 594 times

0

I am making a program, which prints at the end the numbers typed by the user in ascending order using Bubble Sort. It wanted a help to implement this function.

My code:

#include <stdio.h>
#define MAX 10
int main()

   {
  int vetor[MAX], i,t,a;
   t=0;


   for(i=0; i<MAX; i++){
   printf("Informe valor:", vetor[i]);
   scanf("%d", &vetor[i]);

    }

        for(i=0; i<MAX; i++){
      printf("%d\t", vetor[i]);
     }

     do{
    for (i=0; i<MAX-1; i++){
        if(vetor[i]>vetor[i+1]){
        a= vetor[i];
        vetor[i]=vetor[i+1];
        vetor[i+1]=a;
        t=1;
        }
    }

        }while(t==1);
       printf("Os numeros em ordem e: %d", vetor[i]);

      }

2 answers

1

You can implement the Bubble Sort using two for and an auxiliary variable which in this case is the variable a, and apply the method of substitution of values by the largest by the following expression: vetor[t] > vetor[t + 1] then replace the numbers in your variable vetor. See how the modifications turned out:

#include <stdio.h>

#define MAX 5

int main(void)
{
    int vetor[MAX], i, t, a;

    /*Inicializa vetor*/
    for (i = 0; i < MAX; i++)
        vetor[i] = 0;

    for(i = 0; i < MAX; i++)
    {
        printf("Informe valor: "); /*Removi o parametro que estava aqui.*/
        scanf("%d", &vetor[i]);
    }

    /*Bubble Sort*/
    for (i = 0; i < MAX - 1; i++)
    {
        for (t = 0; t < MAX - i - 1; t++)
        {
            if (vetor[t] > vetor[t + 1])
            {
                a = vetor[t];
                vetor[t] = vetor[t + 1];
                vetor[t + 1] = a;
            }
        }
    }

    printf("Os numeros em ordem crescente: ");

    for (i = 0; i < MAX; i++)
        printf("%d ", vetor[i]);

    return 0;
}

Entree:

3 4 1 2 5

Exit:

The numbers in ascending order: 1 2 3 4 5

Explanation

First I initialized the vector with zeros, and modified its first loop that populates the vector with values typed by the user, then just needed to apply the Bubble Sort to sort the numbers and then display the array values for the user.

0

My compiler gives warnings in several lines:

pt91088.c:5:34: warning: data argument not used by format string
      [-Wformat-extra-args]
        printf("Informe valor:", vetor[i]);
               ~~~~~~~~~~~~~~~~  ^
pt91088.c:9:22: warning: data argument not used by format string
      [-Wformat-extra-args]
        printf("\t", vetor[i]);
               ~~~~  ^
pt91088.c:20:16: warning: using the result of an assignment as a condition
      without parentheses [-Wparentheses]
    } while (i = 1);
             ~~^~~
pt91088.c:20:16: note: place parentheses around the assignment to silence this
      warning
    } while (i = 1);
               ^
             (    )
pt91088.c:20:16: note: use '==' to turn this assignment into an equality
      comparison
    } while (i = 1);
               ^
               ==
3 warnings generated.

Tip: turn on as many warnings as possible from your compiler and always correct the displayed warnings.

After correcting the warnings (and format to my taste) the code was like this:

#include <stdio.h>

int main(void) {
    int vetor[10], i, a; // variavel t desnecessaria e removida
    for (i = 0; i < 10; i++) {
        printf("Informe valor:"); // removido valor desnecessario
        scanf("%d", &vetor[i]);
    }
    for (i = 0; i < 10; i++) {
        printf("\t%d", vetor[i]); // adicionado %d para imprimir valor
    }
    // ciclo do removido
    for (i = 0; i < 10; i++) {
        if (vetor[i] > vetor[i + 1]) {
            a = vetor[i];
            vetor[i] = vetor[i + 1];
            vetor[i + 1] = a;
        }
    }
    printf("O vetor em ordem crescente e: %d\n", vetor[i]); // acrescentado \n
    return 0;
}

Now we need to implement the Bubble Sort correctly and make the final impression with a loop, as you did in the initial impression.

Browser other questions tagged

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