Problem Bubble Sorting a Vector

I am trying to Bubble Sort a list of names by the length of the names only. The alphabetical part is already solved. The program crashes while running on the black screen. I can't figure out where the problem is.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
string fileName,line;
ifstream infile;
cout<<"Enter the file name and complete path: ";
cin>>fileName;
int Aa=0, count=0;
std::vector<string> A;
infile.open(fileName.c_str());
while(!infile.eof())
{
getline(infile,line);
if(line[count]=='A'||line[count]=='a')
{
A.push_back(line);
A[Aa]=line;
Aa++;
for(int i=1;(i<=strlen(line.c_str()));i++)
{
for (int j=0;j<(strlen(line.c_str())-1);j++)
{
if (A[j+1] > A[j]) // ascending order simply changes to <
{
string temp = A[j]; // swap elements
A[j] = A[j+1];
A[j+1] = temp;
}
cout<<A[j]<<endl;
}
}
}
}
return 0;
}

Seems several questions in the code.
#1, you put a line into vector, and then start the bubble sort on vector, but with length of the line, A[j] may out of range.
#2. if you want to sort by the length of names, A[j+1] > A[j] is incorrect which compare the string but not the length.

What I am thinking is that you can:
first go through the file and put all lines start with 'a' or 'A' into the vector.
and then bubble sort the vector with vector's size, if sort by length, then don't use A[j+1] > A[j] which is string compare, use A[j+1].length() instead.

I am pasting a piece of 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
    // read in names start with 'a' or 'A' into vector A
    //
    while(!infile.eof())
    {
        getline(infile,line);

        if(line[0]=='A'||line[0]=='a')
        {
            A.push_back(line);
        }
    }

    // bubble sort on vector A by length of name
    //
    const size_t size = A.size();
    for(size_t i=0; i<size; ++i)
    {
        for (size_t j=0; j<size-i-1;j++)
        {
            if (A[j+1].length() < A[j].length())// ascending order simply changes to <
            { 
                A[j].swap(A[j+1]);
            }
        }
    }

    // show sort result
    //
    cout << "Sort result:" << endl;
    for (size_t i=0; i<size; ++i)
        cout << A[i] << endl;



Thanks very much for the revision. I am noticing that there are many different ways to do things. But I have one question about line 15 (const size_t size = A.size().) What is the significance of making the type a constant? I really did not want to declare any constant type in the program because the length and arrangement of the input file is unknown. I wanted to make the program very flexible to accommodate the unknown.
This const won't violate program's flexibility. it depends on number of lines start with 'a' or 'A' in the input file.
Just my habit, avoid call size() every time in for loop (which actually can be optimized by compiler), actually you can put A.size() in for statement directly, which is more clear and flexible.
Are you saying that i can delete "const size_t size" and "size_t size" completely from the program? Will the program work with only "A.size()"?
I'm not sure why you are writing your own sort. I believe that you are approaching this problem incorrectly. Clearly you have no requirement that states that you cannot use the STL. Therefore you should be attempting to use std::sort. Write a functor. I'm going to do you a favor because it appears that you might not understand the concept of a functor and it only takes me about 5 minutes to accomplish this task without writing my own bubble sort. Compile this and test it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> 
#include <vector> 
#include <algorithm> 
 
struct SortByLength 
{ 
    bool operator()(const std::string& lhs, const std::string& rhs) 
    { 
	return lhs.size() < rhs.size(); 
    } 
}; 
 
int main() 
{ 
    std::vector<std::string> MyStrings; 
    MyStrings.push_back("Hello Earth!"); 
    MyStrings.push_back("Hello Jupiter!"); 
    MyStrings.push_back("Hello Sun!"); 
    MyStrings.push_back("Hello Mars!"); 
    std::sort(MyStrings.begin(), MyStrings.end(), SortByLength()); 
    return 0; 
}
FYI, I am not sure that the std::vector is the right tool for this job. You might consider using std::list which has its own sort member function. It can also be used with the same functor. The vector is for situations where you need the data to be a contiguous array which I don't believe is a requirement here. if you are interested I will leave it up to you to research std::list and change the above example to use that instead. std::list is definitely more efficient if you want to take my advice and insert the strings first and sort later. The only drawback is that you lose the operator[] function with that and would have to learn how iterators work.
Topic archived. No new replies allowed.