2d Array Bubble Sort

I'm completely stuck. I need to bubble sort two 2d arrays. Basically I need to bubble sort the first 2d array which holds last names. If the last names are the same I need to sort those last names by first name.

Here's my test code, it's really messy since I've been trying everything I can think of to get it to work properly.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using namespace std;
#include <iostream>
#include <iomanip>


int main()
{
	int records = 4;

	char temp[16] = { 0 };
	char temp2[16] = { 0 };
	char last_name[4][16] = { 0 };
	char first_name[4][16] = { 0 };

	cout << "\nEnter Last Name 0: ";
	cin.sync();
	cin.getline(last_name[0], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter First Name 0: ";
	cin.sync();
	cin.getline(first_name[0], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter Last Name 1: ";
	cin.sync();
	cin.getline(last_name[1], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter First Name 1: ";
	cin.sync();
	cin.getline(first_name[1], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter Last Name 3: ";
	cin.sync();
	cin.getline(last_name[2], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter First Name 3: ";
	cin.sync();
	cin.getline(first_name[2], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter Last Name 4: ";
	cin.sync();
	cin.getline(last_name[3], 16);
	cin.sync();
	cout << endl;

	cout << "\nEnter First Name 4: ";
	cin.sync();
	cin.getline(first_name[3], 16);
	cin.sync();
	cout << endl;

	for (int i = 0; i < records - 1; i++)
	{
		if (strcmp(last_name[i], last_name[i + 1]) > 0)
			{
				strcpy_s(temp, last_name[i]);
				strcpy_s(last_name[i], last_name[i + 1]);
				strcpy_s(last_name[i + 1], temp);

				strcpy_s(temp2, first_name[i]);
				strcpy_s(first_name[i], first_name[i + 1]);
				strcpy_s(first_name[i + 1], temp2);
			}
		else if (strcmp(last_name[i], last_name[i + 1]) == 0)
			{
			if (strcmp(first_name[i], first_name[i + 1]) > 0)
				{
				strcpy_s(temp2, first_name[i]);
				strcpy_s(first_name[i], first_name[i + 1]);
				strcpy_s(first_name[i + 1], temp2);
				}
			}
	}

	cout << last_name[0] << ' ' << first_name[0] << ' ' << "\n"
	       << last_name[1] << ' ' << first_name[1] << ' ' << "\n"
	       << last_name[2] << ' ' << first_name[2] << ' ' << "\n"
	       << last_name[3] << ' ' << first_name[3] << ' ' << "\n";

	system("pause");
	return 0;

}

This is only test code and not my full program. So things like the namespace std, and any other shortcuts I have taken are just for the sake of testing before I stick it into my actual program.

Must be bubble sorted, I know there are other types of sorting algorithms but I need to bubble sort this one
Last edited on
closed account (D80DSL3A)
Bubble sort requires 2 nested loops, not just one for loop. If you haven't written a working bubblesort routine yet I recommend getting it to work with something simple like an array of int values first, then adapt it to this problem.

Two other problems I see.
Exceeding array bounds in the for loop. Go to i < records - 1;. Otherwise i+1 goes too high.
You will also need to swap last names when sorting by first name.

Why is records type char? It probably works but it seems like asking for trouble. Suggest type int.
I have made a working bubble sort on a 1d array. I'm just not sure how to make it work with multiple 2d arrays. Could you give me an example?

here's the code for my 1d array:
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
using namespace std;
#include <iostream>


int main()
{
	int records = 5;

	char a[5];
	char test[5];

	cin >> anus[0]
		>> a[1]
		>> a[2]
		>> a[3]
		>> a[4];

	cin >> test[0]
		>> test[1]
		>> test[2]
		>> test[3]
		>> test[4];

	for (char i = 0; i < records; i++)
	{
		for (char i2 = 0; i2 < records - i - 1; i2++)
		{
			if (a[i2] > a[i2 + 1])
			{
				char temp = a[i2];
				a[i2] = a[i2 + 1];
				a[i2 + 1] = temp;

				char temp2 = test[i2];
				test[i2] = test[i2 + 1];
				test[i2 + 1] = temp2;
			}
		}
	}

	cout << endl
		<< a[0] << ' ' << test[0] << ' ' << "\n"
		<< a[1] << ' ' << test[1] << ' ' << "\n"
		<< a[2] << ' ' << test[2] << ' ' << "\n"
		<< a[3] << ' ' << test[3] << ' ' << "\n"
		<< a[4] << ' ' << test[4] << ' ' << "\n";


	system("pause");
	return 0;

}


Didn't even catch that bounds issue, thanks for pointing that out.
Why would I need to swap last name when sorting by first name? If the last names are the same would it matter if they get swapped?

crap, I'm actually not sure why I set records to char.
Last edited on
closed account (D80DSL3A)
Why would I need to swap last name when sorting by first name? If the last names are the same would it matter if they get swapped?

Good point. I wasn't thinking.

So, what happens if you just put your code for name swapping (lines 65-83 in your 1st post) in place of the code lines 28-37 in your 2nd code? Maybe change to i2 in outer loop and i in the inner loop so you don't have to change all the i's in the name swapping code to i2.

I see your code for bubblesort avoids the array bounds issue already.

I'm actually not sure why I set records to char.

Maybe because you used type char for the loop variables in your 2nd code?
Seems like i'm getting the correct results for now with this code. I just stuck it into my final program as well and it seems to be giving me the correct output. Thanks you so much for all of the help! I can't tell you how much I was stressing out on that problem! You're a life saver!
Topic archived. No new replies allowed.