Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

pessantiago

ajuda bubble sort

Recommended Posts

pessantiago

#include <stdio.h>
int i,j,q;
        q=4;
     //quantidade de numeros de entrada
    char num [4]= {3,1,4,2};
    int temp;

void ordena()
    {


    for(j=q;j>0;j--){
        for(i=0;i<j;i++){
            if(num[ i ]>num[i+1]){
                temp = num[ i ];
                num[ i ] = num[i+1];
                num[i+1] = temp;
            }
        }
    }

    //Imprime o vetor ja ordenado


    }

int main(){




ordena();
puts("\n");
    for (i=0;i<q;i++){
        printf(" %d",num[ i ]);
    }



}

a ordenação do vector devia ser 1,2,3,4 mas nao a ordenação que me parece é 0,1,2,3

sugestoes!!obrigado

Share this post


Link to post
Share on other sites
mogers

Os índices no vector vão de 0 a q-1

for(j=q-1;j>=0;j--){


"What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine

Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.