BUBBLE SORT HELP

Jul 21, 2016 at 12:20am
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;
}
Jul 21, 2016 at 12:53am
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;
			}
		}
	}
Jul 21, 2016 at 12:54am
Does that help you? :)
Jul 21, 2016 at 2:56am
Jul 21, 2016 at 4:24pm
That didnt really help.
Jul 21, 2016 at 4:28pm
closed account (E0p9LyTq)
The 2nd loop sorts the grades, by bubble sort, the 3rd prints out the list.

Now, was that so hard?
Jul 21, 2016 at 4:50pm
chill with the attitude. im new to this.
Jul 22, 2016 at 2:39pm
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 Jul 22, 2016 at 2:40pm
Jul 22, 2016 at 2:59pm
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 Jul 22, 2016 at 3:00pm
Jul 22, 2016 at 7:02pm
Thanks that helped
Jul 23, 2016 at 3:58am
welcome :)
Jul 24, 2016 at 9:35pm
What kind of projects can i do that involve bubble sort.
Jul 24, 2016 at 9:51pm
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.
Jul 25, 2016 at 1:25am
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...
Jul 25, 2016 at 3:05am
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
Jul 25, 2016 at 3:14pm
So learning those three sorting methods is more beneficial?
Jul 25, 2016 at 3:40pm
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.