Help with a Bubble Sort

I have a bubble sort that takes 10 numbers. Before I modularized it it worked fine, but somehow when I created my modules this strange error occurs: The highest value always gets placed into values[1] as 0. For example, if the user types in "1,2,3,4,5,6,7,8,9,10" the program would output "1,0,2,3,4,5,6,7,8,9." I've been at this for hours with no success and I can't find any logic or syntax errors. Please help! Here's 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
55
56
57
58
59
60
61
62
63
64
65
#include <cstdlib>
#include <iostream>

using namespace std;

const int SIZE = 10;
int values[SIZE];

//user inputs data
int read()
{
    int countRead;
    
    for (countRead = 0; countRead < SIZE; countRead++)
    {
        cout << "Enter an integer value: " << endl;
        cin >> values[countRead];
    }
}

//the actual bubble sort module
int sort()
{
    int temp;
    int inner;
    int outer;
    
    for (outer = 0; outer < SIZE - 1; outer++)
    {
        for (inner = 0; inner < SIZE - outer; inner++)
        {
            if (values[inner] > values[inner + 1])
            {
               temp = values[inner];
               values[inner] = values[inner + 1];
               values[inner + 1] = temp;
            }
        }
    }
}

//the ouput
int print()
{
    int countPrint;
    cout << endl << "Sorted array:" << endl;
    
    for (countPrint = 0; countPrint < SIZE; countPrint++)
    {
        cout << values[countPrint] << endl;
    }
}


int main()
{    
    cout << "This program will ask you to enter 10 values, " << endl << "then it will print them out in sorted order." << endl << endl;
    
    read();
    sort();
    print();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
I think it is a fencepost error on line 28.

for (outer = 0; outer < SIZE - 2; outer++)
(See next post)

BTW, you should try to update your sort() function to take arguments:

void sort( int values(), int SIZE )

Then you can have

1
2
3
4
5
6
7
int main()
{
  int values[10];

  ...

  sort( values, 10 );

While you are at it, you can update read() and print() similarly.

1
2
int read( int values[], int MAX_SIZE );  // returns number of values actually read
void print( int values[], int SIZE );

Hope this helps.
Last edited on
Inner loop termination condition should be inner < SIZE - outer - 1. Outer loop termination is OK IMO.

The reason you are getting zero is because 10 gets swapped with the undefined value at values [SIZE], zero in your case. This zero is brought to the beginning of the array by the bubble sort. Give it one more outer loop iteration and you will see that it goes to position 0.
Last edited on
Yes, the fencepost is on the inner loop, not the outer. Good eye!
Wow, thanks a ton, that fixed it. Just to help me understand it:

1.) Why was the inner loop only causing a fencepost error when I divided the program into modules? When I wrote the program all in int main() it worked fine.

and 2.) Why was the undefined value (0) being stored in values[1] instead of values[0]?
Thanks Duoas!

@OP
In such array traversals, you have to be careful about boundary conditions. The values array has a size SIZE.
When your outer index is zero, the inner index loops till SIZE - 1.
Hence inner + 1 = SIZE. Within the loop, you are trying to access values[inner + 1]. values[SIZE] is undefined.
Hence what you were seeing was undefined behaviour.
Thanks, that clarifies things
Topic archived. No new replies allowed.