Bubble sorting a classed array

I'm really struggling with bubble sorting a classed array(an inventory). I understand bubble sorting in theory and implementing it into a static array makes sense. When I get to doing it in a classed array then I struggle. I need to sort the weight by bubble sort (then selection sort for name, and insertion sort the cost; but I want to tackle one problem at a time.)

Here is 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <iomanip>
#include <conio.h>
#include "Inventory.h"
#define MAX_REC 10

using namespace std;

//Class to hold inventory variables/funcitons
class Inventory{
private:

public:
	
	//Placing these here to see if method will work//
	char itemName[15];
	int Cost;
	int Weight;
	/////////////////////////////////////////////////

	void getData();
	void showData();
	
};

//Gathers data from user to populate the inventory
void Inventory :: getData(){
	cout << "\nEnter Item Name: ";
	cin >> itemName;
	cout << "Enter Cost: ";
	cin >> Cost;
	cout << "Enter Weight: ";
	cin >> Weight;
};


void Inventory :: showData(){
	cout << endl;
	cout.width(15);
	cout.setf(ios::left, ios::adjustfield);
	cout << itemName;

	cout.width(8);
	cout << Cost;

	cout.width(15);
	cout.setf(ios::right, ios::adjustfield);
	cout.setf(ios::showpoint);
	cout.setf(ios::fixed, ios::floatfield);
	cout.precision(2);
	cout << Weight;

};

int main(){
	Inventory record[MAX_REC];
	int i, n;

	cout << "\n=====Inventory Management=====\n";
	cout << "\nHow many Records to be created : ";
	cin >> n;

	cout << "Enter " << n << " Record(s)\n";
	for(i=0;i<n;i++)
		record[i].getData();

	cout<<"\n\n---Stock Information---\n";
    cout<<"\n"<<setw(8)<<"Item Name"
        <<setw(10)<<"Cost"
        <<setw(19)<<"Weight"<<endl;
    cout<<"-------------------------------------------";

    for(i=0;i<n;i++)
		record[i].showData();


	/* Commenting out to make another attempt at bubble sort
	////Bubble Sort Code////
	int tempHolder = -1;
	int end = 10;
	int length = 10;

	for(int counter = length - 1; counter > 0; counter--)
	{
		for(int index = 0; index < end; index++)
		{
			if (record[weight] > record[index + 1])
			{
				tempHolder = record[index +1];
				record[index + 1] = record;
				record[index] = tempHolder;
			}
		}

		for(int index = 0; index < 10; index++)
		{
			cout << record[index] << ", ";
		}

		cout << endl;
		6
		end--;
	}

	for(int index = 0; index < 10; index++)
	{
		cout << record[index] << ", ";
	}
	
	cout << endl;
	////////////////////////

	*/

	// Bubble Sort Take 2 //



	/////////////////////////


    _getch();

	return 0;

}
Your bubble sort isn't.

There should be only two loops -- an outer and an inner.
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/

You also seem to have quite a few extraneous names for things: "length", "end"; "counter", "index" -- these are all generic and easily confused. Pick one and stick with it:

1
2
3
4
5
6
#define MAX_REC 10  /* fine -- maximum size of array */

int main()
{
    Inventory inventory[ MAX_REC ];  // the array represents your inventory, which is all records, not just one
    int num_records = 0;  // number of records actually used in the array. max value == MAX_REC, right? 

Now you are ready to write the outer loop:

1
2
3
4
    for (int n = 0; n < (_____ - 1); n++)
    {
        ...
    }

The inner loop will look very much the same. Don't worry about any optimizations.

1
2
3
4
5
6
7
    for (int n = 0; n < (_____ - 1); n++)
    {
        for (int m = 0; m < (_____ - 1); m++)
        {
            ...
        }
    }

Inside the inner loop you compare the current and the next element (at indices m and m+1) to determine whether or not to swap them.

Since the elements are Inventory items, you can compare them by their cost or weight or name or any criteria you want. Just so long as you can figure out that they are out of order (and need to be swapped).


Bubble sort is evil in many myriad ways, so I know it takes a while to wrap your brain around it. Good luck!


[edit] BTW, when you are ready to apply some optimizations, you need only to add two things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    for (int n = 0; n < (_____ - 1); n++)
    {
        bool swapped = false;  // have any items been swapped? (so far this loop, no)

        for (int m = 0; m < (_____ - ___ - 1); m++)  // what else should I subtract here?
        {
            if (...)
            {
                ...
                swapped = true; // yes
            }
            
        }

        if (!swapped) break;  // if no items were swapped, we're done
    }

Hope this helps.
Last edited on
So how would I grab the variable (Weight) to sort?

I'm thinking like this

 
for (int  m = 0; m < Inventory::Weight - 1; m++)


and it's not right so it has the red line underneath it. Should I make a pointer to the variable or is that just wrong to?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	/*
	//// Bubble Sort ////
	/////////////////////
	int temp = -1;
	
	for (int n = 0; n < numRec - 1; n++)
	{
		for (int  m = 0; m < Weight - 1; m++)
		{
			temp = record[m + 1];
			record[m + 1] = record;
			record[m] = temp;
		}
		
	}

	/////////////////////
	//////// End ////////
	*/



I know this isn't right. I'm just having difficulty wrapping my head around this.
What does weight have to do with the length of the array?
Weight is what I'm trying to sort by. Figured it out so all set.


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
//// Bubble Sort ////
	/////////////////////
	Inventory temp;
	
	for (int n = 0; n < numRec - 1; n++)
	{
		
		for (int  m = 0; m < numRec - 1; m++)
		{
			
			if (record[m].Weight > record[m +1].Weight)
			{
				temp = record[m + 1];
				record[m + 1] = record[m];
				record[m] = temp;
			}

		}

		//return;
	}
	
	cout<<"\n\n--------Bubble Sorted Inventory---------\n";
    cout<<"\n"<<setw(8)<<"Item Name"
        <<setw(10)<<"Cost"
        <<setw(19)<<"Weight"<<endl;

	cout << endl;
    cout << "-------------------------------------------";

	for(i = 0; i < numRec; i++)
		record[i].showData();

	cout << endl;
	cout << "--------------------------------------------";

	/////////////////////
	//////// End ////////
Good job!
Topic archived. No new replies allowed.