bubble sort

Hi there,
I am trying to get my head around the bubble search and even if it is pretty much clear how it works in theory, I have some problems when it comes down to write some code and understanding exactly what it does.
here is the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//10/05, yet another array with bubble sort
#include <iostream>
using namespace std;

const int Maximum=10;

int main()
{
int x;
int f;
int Array[Maximum]={32,23,1,0,43,99,11,9,4,100};
for ( x=0;x<Maximum;x++)
	{
	
	cout <<" "<<Array[x]<<" ";
	}
//try the bloody bubble sort again...good luck to me!
for (x=0;x<Maximum;x++)//why can't I remove this one or not?(first comment)
	for(f=Maximum-1;f>=x;--f)//Why do we want f>=x??
	
		if (Array[f-1]>Array[f])
		{
			int Temp=Array[f-1];
			Array[f-1]=Array[f];
			Array[f]=Temp;
			cout<<"Print the new Array "<<Array[f]<<" ";
		}

for(x=0;x<Maximum;x++)
cout<<"\n "<<Array[x]<<" ";
//yay!!!

return 0;
}


Now, here are my questions and observations:
-Line 19:why do we need f>=x;in the second for loop?
-Line 26: what I wanted to do with this line was to be able to print all the passages of the bubble sort when it is executed, but I am not sure if that is the right code. If it is easier, I would love if somebody can go through this bubble sort example step-by-step and help me to understand all the comparisons.
-Line 19: why do we need to create another index, make sure it starts from 9 and progressively decrement it? I really don't understand that

Also, here is my understanding of the program, please correct me if I am wrong starting from line 18:
-when x (which is the index therefore goes from 0 to 9 because the array has 10 elements), is = 0 and smaller than 10 (again it hs to be <10 because the index starts from 0) increment x of 1
-line 19:then for f equal 9 and f greater than or equal x, then decrement f of 1
-line 21:if we said for(f=Maximum-1;f>=x;--f) does it mean that the f-1 on line 21 is initially element n.8 of the Array[f] and that f is element n.9 of the Array[f]? then how does the comparison continue?
can anyone enlighten me please?
thanks
Wiki has a good explanation including a C++ implementation.

http://en.wikipedia.org/wiki/Bubble_sort
A little whitespace goes a long way in terms of readability.
I had a look at wikipedia already thanks, but the sort is done the other way round. What really bugs me in my program is the fact that I don't seem to get the right sequence of the comparisons.
I have Visual C++ installed on my machine and I used the debug option to better understand the sequence.

Basically Line 18 to 27 does the comparison and, given the arrays
values 32,23,1,0,43,99,11,9,4,100, it starts comparing 100 and 4. 4<100 so it skips
line 22-27 and no swap occurs.

Then it compares 4 and 9. 9>4 so it
goes through line 22-27 and the swap occurs resulting in
(32,23,1,0,43,99,11,4,9,100,).

Now 3 and 11. 11>40 so the swap occurs and we have
32,23,1,0,43,99,4,11,9,100.

Now 4 and 99: 99>4 so the swap occurrs and we have
32,23,1,0,43,4,99,11,9,100.
then it goes all teh way down to 32 and the first pass ends leaving my array like
0,32,23,1,4,43,99,11,9,100.

Now this is the first pass, my understanding is that the second pass starts again from the bottom, comparing 100 and 9: 9<100 so no swap occurs and move to 9 and 11. 11>9 so the swaps occurs...and so on. But by looking at the debug option the second pass doesn't seem to start comparing 100 and 9...si what does it compare?
What happens now? It should start again from the bottom
so it should compare 43 and 1 but by looking at the debug option in
Microsoft Visual C++ that's not the case...I get lost. what happens
then? what's the sequence?
thanks
Topic archived. No new replies allowed.