Using bubble sort

Hi. I'm very new to C++, and I cannot seem to figure this problem out. This code is just supposed to have the user enter a list of animals (no more than 25 animals, 30 letters long), then it will output them in the order they were entered and then also in alphabetical order (using bubble sort). I apologize if my code is hard to follow or understand.

Whenever I put in animals such as "dog", "cat", "fish" and terminate the list with a period, sometimes I get "Unhandled exception at 0x55b6d2f3 in CharacterStrings.exe: 0xC0000005: Access violation reading location 0xcccccccc." but then other times I get "A buffer overrun has occurred in CharacterStringsList.exe which has corrupted the program's internal state."

I'm just very confused as to how to fix this. I tried to Google both of them but I couldn't seem to find any solution that would work for me (as I'm still a beginner and don't know how to do much of the more advanced code). I'm pretty sure the error is somewhere in my swap function (maybe?) but I can't seen to figure it out. Maybe a logic error? I've tried everything I can think of.

If you have questions or anything, let me know. Any help or suggestions would be much appreciated! Thank you in advance :)

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

void getInput(char animals[][30]);
void outputLists(char animals[][30]);
void bubbleSort(char animals[][30]);
void Swap(char *a, char *b);


void getInput(char animals[][30])
{
	cout << "Please enter a list of animals, terminated by a period: " << endl;

	for(int i = 0; i < 25; i++)
	{
		cin >> animals[i];
		if(strcmp(animals[i], ".") == 0)
			break;
	}
}

void bubbleSort(char animals[][30], int n)
{
	for(int i = 0; i < n-1; i++)
		for(int j = 0; j < n-1; j++)
			if(animals[j] < animals[j+1])
				Swap(animals[j], animals[j+1]);
}

void Swap(char *a, char *b)
{
	char t[30];
	strcpy(t, a);
	strcpy(a, b);
	strcpy(b, t);
}

void outputLists(char animals[][30])
{
	//Output original list
	for(int i = 0; i < 25; i++)
	{
		if(strcmp(animals[i], ".") == 0)
			break;
		cout << animals[i] << endl;
	}

	//Output sorted list
	for(int i = 0; i < 25; i++)
		cout << animals[i] << endl;
}

int main()
{
	char animals[25][30];

	getInput(animals);

	cout << endl;
	bubbleSort(animals,25);

	outputLists(animals);
	

	cout << endl;
	system("pause");
	return 0;
}
Last edited on
I wasn't able to replicate your problems on my system, but I see that your program has a few problems:

You've assumed your array to always contain 25 items, even though you don't enter 25 animals all the time. Your getInput() function should count how many animals are entered and return the value. On my system, all the other values in the array are uninitialized and print out as garbage. Maybe that's what's causing the crash on your system.

A second problem here is your comparison in bubbleSort():if(animals[j] < animals[j+1]). Your comparison here isn't actually comparing strings, but comparing addresses. To compare strings, you have to use strcmp(),

A third problem is that your original and sorted list are the same array, and so when you finally get to printing you'll print the sorted list twice. If you want to preserve the original array, you'll have to make a copy of it and call bubbleSort() on the copy

Finally, your bubblesort implementation automatically assumes a worst-case number of passes over your array. I suggest replacing your outer for loop with a while loop that checks whether anything was swapped in the last pass over the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool swapped = true;
while (swapped)
{
    swapped = false;
    for (int i = 1; i < n; ++i)
    {
        if (outOfOrderCheck)
        {
            Swap();
            swapped = true;
        }
    } 
}


Sorry I can't directly explain the problems you're having.
Last edited on
Topic archived. No new replies allowed.