C++ bubble sort.

In regards to the for loop with '&& flag' I think I see the intended logic. If there was a transaction keep looping until the end of the array otherwise exit. Well it compiles but it does not work as I see the logic. I really like this routine much better than any others I have seen and would like to keep it. Is it possible my compiler treats the '&&' differently than the compiler the author used? Without rewriting it to extensively is there something that can be done?

Thank you kindly

void bubblesort(vector<int> *bb) {
int i,j,temp,flag=1;
int bbLength = bb->size();

  for(i=1;(i<=bbLength) && flag; i++) {
  flag=0;
    for (j=0; j < (bbLength -1); j++) {
      if (bb[j+1] > bb[j]) {
        temp = bb->at(j);
        bb->at(j) = bb->at(j+1);
        bb->at(j+1) = temp;
        flag = 1;
      }
    }
  }
}

What exactly is not working for you? A concrete example work help understand what problem you are encountering.

If one were to compile the example and run it in a debugger like the Eclipse IDE that I use on Fedora you will see the 'for' loop in question only runs one time all the time. Clearly that is not the intended logic and I know almost factually that the use of "&&" is an error although I'm not sure quite yet how to fix it.

Thanks! :b:

Where did you get this example from? Can you provide a copy of your test program?

This is the extent of what I have put together at the moment. It needs work but it should give you a good idea of what I'm aiming for. Thanks for your interest in this.

#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

void bubblesort(vector<int> *bb) {
int i,j,temp,flag=1;
int bbLength = bb->size();

  for(i=1;(i<=bbLength) && flag; i++) {
  flag=0;
    for (j=0; j < (bbLength -1); j++) {
      if (bb[j+1] > bb[j]) {
        temp = bb->at(j);
        bb->at(j) = bb->at(j+1);
        bb->at(j+1) = temp;
        flag = 1;
      }
    }
  }
}

int main() {

vector<int> random;
random.reserve(21);

srand(time(NULL)); //Seed

  for(int a=0;a<21;a++){
  random[a] = rand() % 20 + 1;
  }
  for(int a=0;a<random.size();a++) {
  cout << random[a] << endl;
  }

  //I really wrote all this crap to do this with it
  bubblesort(&random);

  for(int a=0;a<random.size();a++) {
  cout << random[a] << endl;
  }
return 0;
}