Bubble Sort

closed account (1RiLb7Xj)
Hi all! I'm working on a project with a couple of my buddies. We are almost done with the code for our project. However, we noticed that there is a problem with our bubble sort. We've tried searching for possible helpful answers, but have not come across anything meaningful. If you have any tips or ideas please feel free to share. :)

We believe the problem begins on either line 62-82 or lines 163-193.

Any tips are very much appreciated.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const double CREDIT_LIMIT = 1500.00;

struct CreditCard
{
    string CCN;
    string LastName;
    string FirstName;
    double Balance;
};
    void ChargeToCard(CreditCard& CC, double money);
    void print(const CreditCard CC);
    void bubbleSort(CreditCard ccArr[], int flag, int len);
    int LinearSearch(const CreditCard ccArr[], const CreditCard& CC, int flag, int len);
    void print(const CreditCard arr[], int len);
    void makePaymentToCard(CreditCard& CC, double money);

    const int MAX = 100;

int main()
{
        CreditCard CC1 = { "JIM", "JONES", "56738", 0.0 };
        ChargeToCard(CC1, 200.99);
        CreditCard CC2 = { "ADAM", "ASHLEY", "12345", 0.0 };
        ChargeToCard(CC2, 2000.00);
        CreditCard CC3 = { "BERTHA", "MAPOS", "34567", 0.0 };
        ChargeToCard(CC3, 800.91);
        CreditCard CC4 = { "LISA", "BRAVE", "98765", 0.0 };
        ChargeToCard(CC4, 1001.23);
        CreditCard CC5 = { "WILLY", "NILLY", "23413", 0.0 };
        ChargeToCard(CC5, 700.00);
        CreditCard CC6 = { "JILL", "QUIRK", "67895", 0.0 };
        ChargeToCard(CC6, 1400.91);
        CreditCard CardArray[MAX];
        CardArray[0] = CC1;
        CardArray[1] = CC2;
        CardArray[2] = CC3;
        CardArray[3] = CC4;
        CardArray[4] = CC5;
        CardArray[5] = CC6;

        bool done = false;


        while (!done)
        {
            cout << "Enter the menu item number below and then press enter key." << endl
                << "1. Print credit card list in its current state.\n"
                << "2. Sort based on last name.\n"
                << "3. Sort based on credit card number\n"
                << "4. Sort based on balance.\n"
                << "5. Search based on last name.\n"
                << "6. Search based on credit card number.\n"
                << "7. Exit.\n";
            int choice;
            cin >> choice;

            if (choice == 7)
            {
                done = true;
            }
            else if (choice == 1)
            {
                print(CardArray, 6);
            }
            else if (choice == 2)
            {
                bubbleSort(CardArray, 1, 6);
            }
            else if (choice == 3)
            {
                bubbleSort(CardArray, 2, 6);
            }
            else if (choice == 4)
            {
                bubbleSort(CardArray, 3, 6);
            }
            else if (choice == 5)
            {
                cout << "Enter last name of card holder in all capitals [For example adams must be entered as ADAMS: ";
                string Lname;
                cin >> Lname;
                CreditCard Temp = { "", Lname, "" };
                int index = LinearSearch(CardArray, Temp, 2, 6);

                if (index >= 0)
                {
                    cout << "The record found. Complete detail is below.\n";
                    print(CardArray[index]);
                }
                else
                {
                    cout << "Record with name " << Lname << " not found.\n";
                }
            }
            else if (choice == 6)
            {
                cout << "Enter Credit card number: ";
                string CCN;
                cin >> CCN;
                CreditCard Temp = { "", "", CCN };
                int index = LinearSearch(CardArray, Temp, 2, 6);
                if (index >= 0)
                {
                    cout << "The record found. Complete detail is below.\n";
                    print(CardArray[index]);
                }
                else
                {
                    cout << "Record with card number " << CCN << " not found.\n";
                }
            }
            else
            {
                cout << "This menu item is not yet implemented.\n";
            }
        }
}
void print(const CreditCard CC)
{
    cout << fixed << showpoint << setprecision(2)
        << "Credit Card Number: " << CC.CCN << endl
        << "Name: " << CC.FirstName << ' ' << CC.LastName << endl
        << "Amount owed: $" << CC.Balance << endl
        << "-------------------------------------------" << endl;
}
void print(const CreditCard arr[], int len)
{
    for (size_t i = 0; i < len; i++)
    {
        print(arr[i]);
    }
}
int LinearSearch(const CreditCard ccArr[], const CreditCard& CC, int flag, int len)
{
    int index = -1;

    for (size_t i = 0; i < len; i++)
    {
        if (flag == 1)
        {
            if (ccArr[i].LastName == CC.LastName)
            {
                index = static_cast<int>(i);
                return index;
            }
        }
        else if (flag == 2)
        {
            if (ccArr[i].CCN == CC.CCN)
            {
                index = static_cast<int>(i);
                return index;
            }
        }
    }
    return index;
}
void bubbleSort(CreditCard ccArr[], int flag, int len)
{
    for (size_t i = 0; i < len; i++)
    {
        for (size_t j = 0; j < len- 1-i; j++)
        {
            if (flag == 1)
            {
                if (ccArr[j].LastName > ccArr[j + 1].LastName)
                {
                    string buffer = ccArr[j].LastName;
                    ccArr[j].LastName = ccArr[j + 1].LastName;
                    ccArr[j + 1].LastName = buffer;
                }
            }
            else if (flag == 2)
            {
                if (ccArr[j].CCN > ccArr[j + 1].CCN)
                {
                    string buffer = ccArr[j].CCN;
                    ccArr[j].CCN = ccArr[j + 1].CCN;
                    ccArr[j + 1].CCN = buffer;
                }
            }
            else if (flag == 3)
            {
                if (ccArr[j].Balance > ccArr[j + 1].Balance)
                {
                    double buffer = ccArr[j].Balance;
                    ccArr[j].Balance = ccArr[j + 1].Balance;
                }
            }
        }
    }
}
void ChargeToCard(CreditCard& CC, double money)
{
    double availableCredit = CREDIT_LIMIT - CC.Balance;
    if (money <= availableCredit)
    {
        CC.Balance += money;
    }
    else
    {
        cout << "Hello " << CC.FirstName << ' ' << CC.LastName << endl;
        cout << "Charging $" << money << " will exceed credit limit. Transaction declined." << endl;
        cout << "----------------------------------------------" << endl;
    }
}
void makePaymentToCard(CreditCard& CC, double money)
{
    CC.Balance -= money;
}
Hello bryanvela17,

In your "bubbleSort" function the first problem I see is for (size_t i = 0; i < len; i++). "size_t" is an "unsigned integer type" and "len" is a "signed int". this is a type mismatch. Also see https://en.cppreference.com/w/cpp/types/size_t

With out testing it I am not sure if j < len - 1 - i; would even work. It looks like to much subtraction.

Inside the for loops and based on the setting of "flag" the if statements like
if (ccArr[j].LastName > ccArr[j + 1].LastName) is OK, but inside the if statement you need to swap the whole element (struct) not an individual field in the struct.

This is not an exact match, but might help http://www.cplusplus.com/forum/beginner/231492/#msg1044755

Andy
Andy's right. Your bubble sort needs to be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum SortType { SortByName, SortByCCN, SortByBalance };

void bubbleSort(CreditCard ccArr[], SortType type, size_t len)
{
    for (size_t i = 0; i < len; ++i)
        for (size_t j = 0; j < len - 1 - i; ++j)
        {
            CreditCard &a = ccArr[j], &b = ccArr[j + 1];
            if ((type == SortByName    && a.LastName > b.LastName) ||
                (type == SortByCCN     && a.CCN      > b.CCN     ) ||
                (type == SortByBalance && a.Balance  > b.Balance ))
                    swap(a, b);
        }
}

And you have problems keeping the order of your struct members straight, so you need to fix that. You can't just set them in any order and expect the values to magically go to the right fields. Maybe move the CCN to the third position and fix up your initializations to put the last name first.

@Andy, len - 1 - i does look strange, but since i can only be max len-1 itself, the lowest the expression can go is 0, which is a no-op for the loop body. I suppose i should be bounded by len - 1.
Last edited on
Topic archived. No new replies allowed.