Strange function output

Hi
I have two array sorting functions, bubbleSort and selectionSort. They both sort an array and returns the number swap operations made during the sorting.
When I try to output the result I'm getting a funny problem. When I do this:
1
2
cout  << "Selection sort iterations: " << selectionSort(array, SIZE) << endl;
cout  << "Bubble sort iterations: " << bubbleSort(array2, SIZE)  << endl;

I get a correct output:
1
2
3
hadoque@snal:~/skola/c$ ./9_9
Selection sort iterations: 60
Bubble sort iterations: 258


But when I do like this:
1
2
3
4
    int first = selectionSort(array, SIZE);
    int second =  bubbleSort(array2, SIZE);
    cout  << "Selection sort iterations: " << first << endl;
    cout  << "Bubble sort iterations: " << second  << endl;

I get a faulty output from the second expression:
1
2
3
hadoque@snal:~/skola/c$ ./9_9
Selection sort iterations: 59
Bubble sort iterations: 0


The functions both return int's. Any idea why I get this strange behaviour?
Hmm... can you post the functions for us to look at?
Sure
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
//** Function bubbleSort **
int bubbleSort(int farray[], int fsize)
{
    int i,
      temp;
  int iterations=0;
    bool swap;

    while (swap)
    {
        swap = false;
        for (i=0; i < fsize ; i++)
        {

            if (farray[i] > farray[i+1])
            {
                temp = farray[i];
                farray[i] = farray[i+1];
                farray[i+1] = temp;
                swap = true;
                iterations++;
            }
        }
    }
    return iterations;
}


//** Function selectionSort **
int selectionSort(int array[], int size)
{
    int startScan,
    minIndex,
    minValue,
    iterations=0;

    for (startScan = 0; startScan < (size -1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];

        for (int index = startScan +1; index < size; index++)
        {

            if (array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
                iterations++;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
    return iterations;

}
Ok, I solved the problem by changing the order of the expressions to
1
2
3
4
    int first = selectionSort(array, SIZE);
    cout  << "Selection sort iterations: " << first << endl;
    int second =  bubbleSort(array2, SIZE);
    cout  << "Bubble sort iterations: " << second  << endl;


With this order the output is correct.
But isn't this really strange? Could it be a bug in the compiler?
uisng mingw compiler - I get the same answer no matter which way you order
the expressions. (Now this makes sense to me).

The bubble sort gives iterations = 0;

This is because the bool swap variable in the bubblesort is not initialised (and therefore probably defaults to false )- so the loop never runs.
Oh yeah... Initializing it to true fixes the problem... Still odd behavior , the order still shouldn't matter, right? I use g++.

If I hadn't had correct output at all from bubblesort, I would probably have caught that bug...

Thanks for the help..
The value of uninitialized variables is undefined, so it is neither odd nor not odd.

The bool might be true, might be false. Depends what is on the stack at that memory location.
Topic archived. No new replies allowed.