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....
#include <iostream>
usingnamespace 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)
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.
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...
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"