BUBBLE SORT HELP

Nothing wrong with this code, i just want a quick explanation on the second and third for loops.
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
35
36
  #include <iostream>
using namespace std;

int main() {
	

	int grades[6];
	int j;

	for (int g = 0; g<=5; g++) {
		
		cout << "Enter a number: " << endl;
		cin >> grades[g];
		}
	for (int g = 0; g <= 4; g++) {

		for (j = g + 1; j <= 5; j++)
		{
			int temp;

			if (grades[g] > grades[j]) {
				temp = grades[g];
				grades[g] = grades[j];
				grades[j] = temp;
			}
		}
	}
	
	for (int g = 0; g <= 5; g++) {
		cout << grades[g] << endl;
	}

	
	system("pause");
	return 0;
}
Hi,
1
2
3
4
5
6
7
8
9
10
11
for (int g = 0; g <= 4; g++) {
		for (j = g + 1; j <= 5; j++)
		{
			int temp;
			if (grades[g] > grades[j]) {
				temp = grades[g];
				grades[g] = grades[j];
				grades[j] = temp;
			}
		}
	}


==>
1
2
3
4
5
6
7
8
9
10
11
for (int g = 0; g <= 5; g++) {
		for (j = 0; j < 5; j++)
		{
			int temp;
			if (grades[j+1] > grades[j]) {
				temp = grades[j];
				grades[j] = grades[j+1];
				grades[j+1] = temp;
			}
		}
	}
Does that help you? :)
That didnt really help.
closed account (E0p9LyTq)
The 2nd loop sorts the grades, by bubble sort, the 3rd prints out the list.

Now, was that so hard?
chill with the attitude. im new to this.
It takes time for this kind of stuff to make sense.
Bubble sort, in particular, isn't necessarily as obvious as many people claim.

Bubble sort consists of two nested loops.

The inner loop (line 17) goes through and finds adjacent pairs and fixes them:

    6 5 4 3 2 1
→  [5 6]4 3 2 1
→   5[4 6]3 2 1
→   5 4[3 6]2 1
→   5 4 3[2 6]1
→   5 4 3 2[1 6]


You can also see how the 6 "bubbled" to its final location. (And all the other numbers have moved closer to where they belong.)

The outer loop repeats this process as many times as necessary for every element to be "bubbled" to its final location.

Go to the link again and watch the little animation.

Hope this helps better.
Last edited on
basically (watch the animation given by Duoas it will be more clear) the 2nd loop will evaluate every object in array and the third loop will evaluate the objects whose index is greater than that of second index....

Here's a slight tweak which might help you

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
 #include <iostream>
using namespace std;

int main() {
	

	int grades[6];
	int j;

	for (int g = 0; g<=5; g++) {
		
		cout << "Enter a number: " << endl;
		cin >> grades[g];
		}
	for (int g = 0; g <= 4; g++) {

		for (j = g + 1; j <= 5; j++)
		{
			int temp;

			if (grades[g] > grades[j]) {
				temp = grades[g];
				grades[g] = grades[j];
				grades[j] = temp;
			}
		for (int g = 0; g <= 5; g++) {
		cout << grades[g];
	                                      }
	 cout<< endl;
		}
		
	
	}



Enter a number: 
5
Enter a number: 
7
Enter a number: 
3
Enter a number: 
5
Enter a number: 
2
Enter a number: 
76
5735276
3755276
3755276
2755376
2755376
2575376
2575376
2375576
2375576
2357576
2357576
2357576
2355776
2355776
2355776
 
Exit code: 0 (normal program termination)
Last edited on
Thanks that helped
welcome :)
What kind of projects can i do that involve bubble sort.
Any thing that involves sorting a list of things.

Imagine a class roll that needs to be printed in alphabetical order by student name.
Or a program that gives nearby hotel prices from cheapest to most expensive.

Use your imagination.
A set of cards in hand to be sorted is a classic example of bubble sort... Although we all do this unconsciously and without the knowledge of underlying algorithm stuff...
It is my opinion that a bubble sort is really a very weird algorithm -- neither easy to understand nor very useful.

Much easier to play with, in order:
-- insertion sort and selection sort
-- merge sort
-- quicksort
So learning those three sorting methods is more beneficial?
In a way yes... learning any algorithm is beneficial to you (and ergo to society) , you never know when/where you can use what... I would recommend you to try to understand them and not just learn them for the sake of "some-weird-stuff"
Topic archived. No new replies allowed.