Bubble Sort Problem

Hello I created a simple bubble Sort program after trying c++ I am really new to coding and I tried learning myself but Ive been facing some problems. Basically this bubble Sort allows you to put your own numbers and how many numbers you want to sort

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

int main ()
{
    int num;
    int index;
    
  cout << "Please type in how many numbers"
           << endl << "you want to sort" << endl;
    cin>>num;
    int myArray[num];
    
     cout << "Please type the numbers you want to sort" << endl;
    
    for (index=0;index<num;index++)
   {
     cout << index+1 << ") ";
     cin>>myArray[index];
   }
    
    cout << endl;
 
 int swapholder=-1;
 
    for (index=0;index<num;index++)
   {
     cout << myArray[index] << ", ";
   }
     cout << endl;
for (int counter=num-1;counter>0;counter--)
{

     for(index=0;index<num+1;index++)
     {
     if (myArray[index]>myArray[index+1])
        {
        swapholder = myArray[index];
        myArray[index] =myArray[index+1];
        myArray[index+1] = swapholder;
        }     
     }

     for (index=0;index<num;index++)
        {
        cout << myArray[index] << ", ";
        }
        cout << endl;
        
}
   

for (index=0;index<num;index++)
   {
     cout << myArray[index] << ", ";
   }
}
for(index=0;index<num+1;index++)

Your mistake is in this line.
Sorry but what's wrong with the line? 😅🙏
The middle bit.
The condition should be index < num, because the last element of your array is index[num - 1].

By the way, some C++ implementations (like Visual C++) don't allow you to use variable-length arrays, i.e. you can't declare int myArray[num];

g++ compiles your code just fine, but I'm not sure if this has been added as an extension. Visual Studio, on the other hand, won't compile unless I rewrite that line of code as
int* myArray = new int[num];
If you're not sure what that line of code does, do a bit of research about pointers and how they relate to arrays.

Your outer loop looks odd to me. I'm not sure why you're cycling from the maximum index down to 0. I've had good success using two nested loops, both going from 0 upwards.
Have a look at this:
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
#include <iostream>
using namespace std;

void printArray(int* a, int size) {
	for (int i = 0; i < size; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
}

void swap(int &a, int &b) {
	int swap = a;
	a = b;
	b = swap;
}

int main()
{
	int num;

	cout << "Please type in how many numbers"
		<< endl << "you want to sort" << endl;
	cin >> num;
	int* myArray = new int[num];

	cout << "Please type the numbers you want to sort" << endl;

	for (int i = 0; i < num; i++)
	{
		cout << i + 1 << ") ";
		cin >> myArray[i];
	}
	cout << endl;
	printArray(myArray, num);
	
	for (int i = 0; i < num - 1; i++)
		for (int j = 0; j < num - i - 1; j++)
			if (myArray[j] > myArray[j + 1]) {
				swap(myArray[j], myArray[j + 1]);
			}

	printArray(myArray, num);

	system("pause");
	return 0;
}
I don't know why but the same problem still occurs. The biggest number becomes 0 during the first sort.
What have you changed line 34 to? We can't comment on code that we can't see.

Note that you will be using myArray[index+1] on line 36, so ask yourself how big index can legitimately be.
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
#include <iostream>
using namespace std;

int main ()
{
    int num;
    int index;
    
  cout << "Please type in how many numbers"
           << endl << "you want to sort" << endl;
    cin>>num;
    int* myArray = new int[num];
    
     cout << "Please type the numbers you want to sort" << endl;
    
    for (index=0;index<num;index++)
   {
     cout << index+1 << ") ";
     cin>>myArray[index];
   }
    
    cout << endl;
 
 int swapholder=-1;
 
    for (index=0;index<num;index++)
   {
     cout << myArray[index] << ", ";
   }
     cout << endl;
for (int counter=num-1;counter>0;counter--)
{

     for(index=0;index<num;index++)
     {
     if (myArray[index]>myArray[index+1])
        {
        swapholder = myArray[index];
        myArray[index] =myArray[index+1];
        myArray[index+1] = swapholder;
        }     
     }

     for (index=0;index<num;index++)
        {
        cout << myArray[index] << ", ";
        }
        cout << endl;
        
}
   

for (index=0;index<num;index++)
   {
     cout << myArray[index] << ", ";
   }
}

Omg I did it!!! it's supposed to be index +1! At line 34!

for (index=0;index+1<num;index++)

Thanks for the advice. No wonder the last number became 0 because index needed to be bigger by 1 due to the swap holder.
It's much more common to say
for (index=0;index<num-1;index++)
This is the standard C way to cycle over an array of size num. Although this being C++, you should really be doing it in modern C++ style. Which basically means don't use arrays, use a vector.

vector theVector(num); // create vector of size num


needed to be bigger by 1 due to the swap holder.

That make no sense at all and shows a significant misunderstanding. The problem was that you were cycling over the array, and then some extra space next to the array. That was the problem. That you had an array of size num, and then you were using it as if it were size num+1.


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;
    int index;
    
    cout << "Please type in how many numbers"
           << endl << "you want to sort" << endl;
    cin>>num;
    int myArray[num];
    
    cout << "Please type the numbers you want to sort" << endl;
    
    for (index=0;index<num;index++)
    {
      cout << index+1 << ") ";
      cin>>myArray[index];
    }
    
    cout << endl;
 
    int swapholder=-1;
 
    for (index=0;index<num;index++)
   {
      cout << myArray[index] << ", ";
   }
    cout << endl;
    for (int counter=num-1;counter>0;counter--)
    {
       for(index=0;index<num-1;index++)
       {
         if (myArray[index]>myArray[index+1])
         {
            swapholder = myArray[index];
            myArray[index] =myArray[index+1];
            myArray[index+1] = swapholder;
          }     
        }

        for (index=0;index<num;index++)
       {
          cout << myArray[index] << ", ";
        }
        cout << endl;  
    }
  
    for (index=0;index<num;index++)
    {
      cout << myArray[index] << ", ";
    }
}


Please type in how many numbers
you want to sort
8
Please type the numbers you want to sort
1) 43
2) 869589
3) -37
4) 2
5) 0
6) -43
7) 1253
8) -9

43, 869589, -37, 2, 0, -43, 1253, -9, 
43, -37, 2, 0, -43, 1253, -9, 869589, 
-37, 2, 0, -43, 43, -9, 1253, 869589, 
-37, 0, -43, 2, -9, 43, 1253, 869589, 
-37, -43, 0, -9, 2, 43, 1253, 869589, 
-43, -37, -9, 0, 2, 43, 1253, 869589, 
-43, -37, -9, 0, 2, 43, 1253, 869589, 
-43, -37, -9, 0, 2, 43, 1253, 869589, 
-43, -37, -9, 0, 2, 43, 1253, 869589,
Last edited on
Oh man looks like I have to go over the code maybe redo it abit so I can get a better understanding.
Topic archived. No new replies allowed.