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.