0

Hi there,

I am trying to get my head around the bubble sort but I don't seem to get it.

Now I have a program written by somebody which performs a bubble sort and I am trying to understand it but no much success. Here is the code:

```
#include <iostream> //This is my teacher's and it works fine.
#include <math.h>
using namespace std;
const int MAX = 10;
void main()
{
int i;
int Data[MAX]; //1D array to hold data
for (i=0; i<MAX; i++)
{
Data[i]=rand() % 101;
cout <<" " << Data[i] << "\n";
}
//sort the data array using the bubble sort
for (i=0; i < MAX; i++)
for (i=1; i < MAX; ++i)
for (int j = MAX - 1; j >=i; --j)
if (Data[j-1] > Data[j])
{
int tmp = Data[j-1];
Data[j-1] = Data[j];
Data[j] = tmp;
}
for (i=0; i < MAX; i++)
cout << Data[i] << "--";
}
```

I am trying to replicate this on my program which I 've just started:

```
#include <iostream>
using namespace std;
const int MAX = 5;
int main ()
{
int Data[MAX] = {51,62,31,27,90};
...
```

but obviously I don't want to copy it, I want to understand it first and then apply it to mine.

I also had a look at wikipedia http://en.wikipedia.org/wiki/Bubble_sort where there is some pseudocode, which helped me a little bit but there are things like flag variables that I haven't covered as yet, so I am not sure how to use them...help please!

thanks