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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````#include #include #include #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 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; } ``````

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:

 ``123456`` ``````#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:

 ``1234`` `````` for (int n = 0; n < (_____ - 1); n++) { ... }``````

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

 ``1234567`` `````` 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!

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

 ``12345678910111213141516`` `````` 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?
 ``12345678910111213141516171819`` `````` /* //// 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````//// 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"<
Good job!
Topic archived. No new replies allowed.