Simplifying bubble Sort

I entered the values:
1 2 3 4 5
After that. the step-by-step output of the program shows:
2 1 3 4 5
3 1 2 4 5
4 1 2 3 5
5 1 2 3 4
1 5 2 3 4
1 2 5 3 4
1 2 3 5 4
1 2 3 4 5

Is it not possible to do nothing if the order is proper?
The inputs above is already in order. For me, it just make it more complicated.

The code:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int main()
{
	int num[100];
	int i, y, z, x;
	for (i = 0; i < 5; i++)
	{
		cout << "Enter a number: ";
		cin >> num[i];
	}
	for (i = 0; i < 5; i++)
	{
		for (z = 0; z < 5; z ++)
		{
			if (num[i] < num[z])
			{
				y = num[i];
				//cout<< "y = " <<y<<endl;
				num[i] = num[z];
				//cout<< "num["<<i<<"] = "<<num[i]<<endl;
				num[z] = y;
				//cout<< "num["<<z<<"] = "<<num[z]<<endl;
				cout<< endl;
                for (x=0;x<5;x++)
                {
                    cout<< num[x]<<" ";
                }
                cout<< endl;
			}
			/*
			else
			{
			    for (x=0;x<5;x++)
                {
                    cout<< num[x]<<" ";
                }
                cout<< endl;
			}
			*/


		}
	}
	cout << "\n\n\nAscending order  ";
	for (i = 0; i < 5; i++)
	{
		cout << num[i];
		cout << " ";
	}
	cout << endl;

	return 0;
}


Also if the input are:
3 4 2 1 5

The result would be:
4 3 2 1 5
5 3 2 1 4
3 5 2 1 4
.....l
1 2 3 4 5

More convenient(But i dont know how)
3 4 2 1 5
3 2 4 1 5
2 3 4 1 5
2 3 1 4 5
2 1 3 4 5
1 2 3 4 5
It is more direct.
Last edited on
That's not how bubble sort works.
A single iteration is
for(i = 0; i < len-1; i++) if( element[i] > element[i+1] ) swap( element[i], element[i+1] );

You have to repeat that until swapping doesn't happen. (Use a boolean to tell)
I know that this isn't a big issue but I just can't stand to see the style of declaring a variable at the start of your programs that is meant for your for-loops but is however being reset for each for-loop. And the way how you made your array and refer to its size is a little sketchy.

Also, how do you run your program? What IDE do you use? I don't see how you're running your program without closing on you but by opening it with cmd/command.com

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

// using namespace std;	// This is really a bad habit I find

using std::cout; using std::cin;
using std::endl;

int main()
{
	static const int numSize = 5; // Making the array a fixed size and using a variable to refer to its size
	int num[numSize];
	// int i, y, z, x; -- You need to get rid of this, if you're going to use the same value
	// throughout your for-loops, then declare before the loops, however use a meaningful name
	// Also when you declare the variable within the for loop, it's only going to be local in that
	// loop scope, so you don't have to worry about for (int i.... mixing up with another
	// int i outside the for loop
	for (int i = 0; i < numSize; ++i)	/* // int i will only be from here ---\/ */
	{
		cout << "Enter a number: ";
		cin >> num[i];
	}					/* // to here ------------------------/\ */
	for (int i = 0; i < numSize; ++i)	// I like using the prefix ++ since its safer sometimes
	{
		for (int z = 0; z < numSize; ++z)
		{
			if (num[i] < num[z])
			{
				int y = num[i];
				//cout<< "y = " <<y<<endl;
				num[i] = num[z];
				//cout<< "num["<<i<<"] = "<<num[i]<<endl;
				num[z] = y;
				//cout<< "num["<<z<<"] = "<<num[z]<<endl;

				cout << endl;
				for (int x=0; x < numSize; ++x)
				{
					cout<< num[x] << " ";
				}
				cout << endl;
			}
			/*
			else
			{
			for (int x=0; x < numSize; x++)
			{
			cout<< num[x]<<" ";
			}
			cout<< endl;
			}
			*/
		}
	}
	cout << "\n\n\nAscending order: ";
	for (int i = 0; i < numSize; ++i)
	{
		cout << num[i];
		cout << " ";
	}
	cout << endl;

	system("pause");	// This stops the program from closing on you when you're debugging it
	return 0;
}
Last edited on
@eNergizer: please read these
Console closing down http://www.cplusplus.com/forum/beginner/1988/
Why system is evil http://www.cplusplus.com/forum/articles/11153/

@mainframe639: what do you want to accomplish?
For almost sorted data you better use insertion sort http://en.wikipedia.org/wiki/Insertion_sort
@ne555: What do you recommend that I should use? I'm worried about the stream being in error state and affecting cin.get().
what do you want to accomplish?
I think he wants to write a bubble sort! ( = the least complex sorting algorithm ).
In case I'm wrong, the right answer would be std::sort.
@eNergizer: I recommend to use an IDE. You should not dirty your code.
If you want to simulates what the IDE does, use a wrapper that will call at your executable http://www.cplusplus.com/forum/beginner/1988/page3.html#msg14102 (for windows)

bubble sort! ( = the least complex sorting algorithm ).
I disagree. The least complex is selection sort (minimum, swap, next)
And std::sort is too general, if you know something about the data (or the initial state of the array) you could do better
@ne555: I'm using Visual Studio 2010, I'm guessing you might be recommending me to use a breakpoint but I want the pause to be included in my console executable. I'm using another way to pause at the moment but if you have a more efficient way, please show me. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>

int main()
{
	std::cout << "Press Enter to Exit ... " << std::endl;

	//in case a badbit or failbit has been set
	std::cin.clear();

	//i prefer this to ignore() because ignore sometimes means hitting enter twice
	std::cin.sync(); 

	std::cin.get();

	return 0;
}
Last edited on
The least complex is selection sort (minimum, swap, next)
Let's say it's a tie..

And std::sort is too general, if you know something about the data (or the initial state of the array) you could do better
You could, but usually you don't really need to. std::sort is fine unless you need super optimized code.

@eNergizer, what ne555 linked to is best, but cin.ignore().get(); (or cin.get(); cin.get() or whatever (twice, becaus if you used cin >> a newline char is in the stream) ) would work fine. You don't need to worry about the error flags. If your code is well written, you'll have handled those before the program ends, right?
Last edited on
Tbh, if you write your program well then cin shouldn't be an error state, and there shouldn't be left over characters. But that's just me...
Tbh, if you write your program well then cin shouldn't be an error state, and there shouldn't be left over characters. But that's just me...

Same here.
Thanks for all replies.
Topic archived. No new replies allowed.