bubbleSort array exception error

Can someone please explain to me what does this error mean?
https://i.gyazo.com/131105ff315fb26da9b026e596b1d31f.png

I have been learning c++ for a long time now but I don't understand this error. I get other compiling errors but what does this one mean?

I am trying to sort some array with bubble 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
#include <iostream>
#include <array>
using namespace std;

void bubbleSort(int arr[])
{
	int size;               // length of arr.
	size = sizeof(arr) / sizeof(*arr);
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}

	cout << arr;
}

int main()
{
	int arr[] = { 6, 7, 9, 2, 11 };
	bubbleSort(arr);
	getchar();
	return 0;
}
Last edited on
You cannot create an array like this. And you can't use sizeof like that. It will give you incorrect answer in the size. Your error is that you are trying to access wrong memory. Try the following change.

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

void bubbleSort(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
        cout << arr;
}

int main()
{
	int arr[5] = { 6, 7, 9, 2, 11 };
	bubbleSort(arr, 5);
	getchar();
	return 0;
}


Next it is much better to use a bool variable to indicate if swapping occurred to save your loop executions. Check this code out.

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
#include <iostream>

using namespace std;

void bubbleSortArray(int [], int);
void displayArray(int [], int);

const int SIZE = 5;

int main()
{
    int values[SIZE] = {9, 2, 0, 11, 5};
    cout << "The values before the bubble sort is performed are:" << endl;
    displayArray(values, SIZE);
    bubbleSortArray(values, SIZE);
    cout << "The values after the bubble sort is performed are:" << endl;
    displayArray(values, SIZE);
    return 0;
}

void displayArray(int array[], int elems)
{
    for(int count = 0;count < elems;count++)
        cout << array[count] << "  ";
    cout << endl;
}

void bubbleSortArray(int array[], int elems)
{
    bool swap;
    int temp, bottom = elems - 1;
    do
    {
        swap = false;
        for (int count = 0; count < bottom; count++)
        {
            if (array[count] > array[count+1])
            {
                temp = array[count];
                array[count] = array[count+1];
                array[count+1] = temp;
                swap = true;
            }
        }
        bottom--;
    }
    while(swap != false);
}
What change? the first code is still showing garbage values.

However, the second code I understood very well. Thank you very much. It's a must to use bool and do while loop when dealing with bubble sort because the idea of bubble sort is to go only once after the first swapping loop. I hope I am not mistaken.

But I have a question: How do I show that this bool swap actually did save me iterations? I want to witness that with my eyes. How can I see it in VS?

Also, I believe that you need two for loops, because we need one whole pass without any swapping to check whether if the list is sorted or not.

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

void toBeSorted(int arr[], int size)
{
	bool swap = false;            
	int temp, bottom = size - 1;
	do
	{
		for (int i = 0; i < size; i++)
		{
			for (int k = 0; k < bottom; k++)
			{
				if (arr[k] > arr[k + 1])
				{
					temp = arr[k];
					arr[k] = arr[k + 1];
					arr[k + 1] = temp;
					swap = true;
				}
			}
		}
		bottom--;
	} while (swap != true);

}

void displayArr(int arr[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << " ";
	}
}

int main()
{
	int arr1[] = { 3,2,4,1,5 };
	cout << "Before Sorting The Array" << endl;
	displayArr(arr1, 5);
	cout << endl;
	cout << "After Sorting The Array:" << endl;
	toBeSorted(arr1, 5);
	displayArr(arr1, 5);

	getchar();
	return 0;
}
Last edited on
Try to run the following code and you will know the difference.

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
66
67
68
69
#include <iostream>

using namespace std;

void bubbleSortArray(int [], int);
void Sortbubble(int [], int);

const int SIZE = 5;

int main()
{
    int values[SIZE] = {5, 4, 1, 2, 3};
    int value[SIZE] = {5, 4, 1, 2, 3};
    cout << "Sorting without the bool variable" << endl << endl;
    Sortbubble(value, SIZE);
    cout << "Sorting with the bool variable" << endl << endl;
    bubbleSortArray(values, SIZE);
    return 0;
}

void bubbleSortArray(int array[], int elems)
{
    bool swap;
    int temp, bottom = elems - 1;
    int iter = 1;
    do
    {
        swap = false;
        cout << "No. " << iter << " iteration of the outer loop." << endl;
        for (int count = 0; count < bottom; count++)
        {
            cout << "No. " << count + 1 << " iteration of the inner loop." << endl;
            if (array[count] > array[count+1])
            {
                cout << "Swapping " << array[count] << " with " << array[count + 1] << endl;
                temp = array[count];
                array[count] = array[count+1];
                array[count+1] = temp;
                swap = true;
            }
        }
        bottom--;
        iter++;
    }
    while(swap != false);
}

void Sortbubble(int array[], int elems)
{
    int temp, bottom = elems - 1;
    int iter = 1;
    for (int j = 0; j < elems; j++)
    {
        cout << "No. " << iter << " iteration of the outer loop." << endl; 
        for (int count = 0; count < bottom; count++)
        {
            cout << "No. " << count + 1 << " iteration of the inner loop." << endl;
            if (array[count] > array[count+1])
            {
                cout << "Swapping " << array[count] << " with " << array[count + 1] << endl;
                temp = array[count];
                array[count] = array[count+1];
                array[count+1] = temp;
            }
        }
        iter++;
        bottom--;
    }
}


And we are using the bool variable to check whether the data is sorted or not. If the data is sorted then a swap will not occur. And hence the swap variable will be false.
Last edited on
Thank you very much for your clarifications :)
No problem
Topic archived. No new replies allowed.