Application of Bubble Sort

0

I am making a program, which prints the numbers entered by the user in ascending order using Bubble Sort. I wanted 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]);

      }
    
asked by anonymous 07.10.2015 / 05:47

2 answers

1

You can implement Bubble Sort using two for and an auxiliary variable that in this case is the variable a , and apply the method of replacing values with the largest expression using the following expression: vetor[t] > vetor[t + 1] then substitute the numbers in its vetor variable. Here's how the changes were made:

#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;
}

Entry:

  

3 4 1 2 5

Output:

  

The numbers in ascending order: 1 2 3 4 5

Explanation

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

    
12.03.2016 / 16:34
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 the maximum warnings from your compiler and always correct the warnings that appear.

After correcting warnings (and formatting to my liking) the code looks 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;
}

Bubble Sort is now correctly implemented and the final print with a loop as you did in the initial print.

    
07.10.2015 / 10:54