see how many times a number changes position in c++ using Bubblesort

Asked

Viewed 71 times

1

Could someone give an example of how I would do that? Type, a size 5 vector, with the following numbers: 33.64.21.85 and 15. I wonder how many times he compares the numbers and performs the exchange, leaving it in ascending order: 15,21,33,64 and 85, then it would return like the value of 9 iterations I think.

while(vet[j]>vet[j+1]){
        contador++;
        if(vet[j]>vet[j+1]){    
        aux=vet[j];             
        vet[j]=vet[j+1];        
        vet[j+1]=aux;           

      }
       }
     }
   }
   cout<<"Iteracoes: "<< contador<<endl;

Is there an error in that while?

  • 1

    Did you implement bubblesort code in C++? It would be easier to explain if you put your code in the question, but briefly, just initialize a counter with zero before the iterations and increment it with each exchange performed.

No answers

Browser other questions tagged

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