简体   繁体   中英

C++: Crashing of a program using Insertion Sort

I am trying to sort a record of employees. This record consists of entries containing the first name, last name and the age of the employee. The entire program is running fine except for certain input statements ( given as comments in the main function ). The program is as follows:-

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class cPerson
{
    private:
    string firstname,lastname;
    int age;
    public:
    cPerson(string fn,string ln,int a)      // constructor to input the firstname, lastname and age of the person
    {
        firstname=fn;
        lastname=ln;
        age=a;
    }
    void display()
    {
        cout<<"First Name = "<<firstname<<"\n";
        cout<<"Last Name = "<<lastname<<"\n";
        cout<<"Age = "<<age<<"\n";
    }
    int getAge()
    {
        return age;
    }
    string getLastName()
    {
        return lastname;
    }
};
class cOffice
{
    private:
        vector<cPerson*> v;
        int nElem;                      // counter of number of elements in the vector;
    public:
        cOffice(int max)
        {
            v.resize(max);              // maximum capacity of the vector
            nElem=0;                    
        }
        ~cOffice()
        {
            for(int i=0;i<nElem;i++)
                delete v[i];
        }
        void insertRec(string fn1, string ln1, int a1)      // inserting the record
        {
            v[nElem] = new cPerson(fn1,ln1,a1);
            nElem++;
        }
        void InsertionSort()
        {
            int compare,pivot;
            for(pivot=1;pivot<nElem;pivot++)
            {
                cPerson* temp = v[pivot];
                for(compare=pivot;v[compare-1]->getAge()>temp->getAge();compare--)
                {   
                    v[compare]=v[compare-1];
                }
                v[compare] = temp;
            }
        }
        void display()
        {
            for(int i=0;i<nElem;i++)
                v[i]->display();
        }
};
int main(void)
{
    cOffice obj(10);
    obj.insertRec("Evans", "Patty", 24); 
    obj.insertRec("Adams", "Henry", 63);
    obj.insertRec("Yee", "Tom", 43);
    obj.insertRec("Smith", "Lorraine", 37);
    /*obj.insertRec("Hashimoto", "Sato", 21);                  // cause of the crash
    obj.insertRec("Stimson", "Henry", 29);
    obj.insertRec("Velasquez", "Jose", 72);
    obj.insertRec("Lamarque", "Henry", 54);
    obj.insertRec("Vang", "Minh", 22);
    obj.insertRec("Creswell", "Lucinda", 18);*/  
    obj.display();
    obj.InsertionSort();
    obj.display();
    return 0;
}

The problem is that the first four input statements are working just fine ( in any order I type them in ). This indicates that my InsertionSort() function is working fine ( is it? ). I want to know what have I done wrong in the program. Thanks for the help.

The corrected InsertionSort function ( as suggested by MM-BB ) is as follow:-

void InsertionSort()
            {
                int compare,pivot;
                for(pivot=1;pivot<nElem;pivot++)
                {
                    cPerson* temp = v[pivot];
                    for(compare=pivot;compare>0&&v[compare-1]->getAge()>temp->getAge();compare--)
                    {   
                        v[compare]=v[compare-1];
                    }
                    v[compare] = temp;
                }
            }

The correction was the addition of the term compare>0 && before the condition in the the second for statement. This ensured that the vector v remained within bounds. Earlier, this was not the case which led to v[-1] which was not defined and hence led to program crash.

 for(compare=pivot;v[compare-1]->getAge()>temp->getAge();compare--) 

Let's say pivot is 1 , compare will be 1 . Then you access element 0 in the list v . If the age is bigger, you will decrease compare, which makes you access an out of bounds element ( v[-1] ).

your function must have compare > 0
two way exist that one of them is true

way 1

void InsertionSort()
{
    int compare,pivot;
    for(pivot=1;pivot<nElem;pivot++)
    {
        cPerson* temp = v[pivot];
        for(compare=pivot; compare > 0 && v[compare-1]->getAge()>temp->getAge();compare--)
        {
            v[compare]=v[compare-1];
        }
        v[compare] = temp;
    }
}

way 2

void InsertionSort()
{
    int compare,pivot;
    for(pivot=1;pivot<nElem;pivot++)
    {
        cPerson* temp = v[pivot];
        for(compare=pivot; v[compare-1]->getAge()>temp->getAge() && compare>0 ;compare--)
        {
            v[compare]=v[compare-1];
        }
        v[compare] = temp;
    }
}

way 1 is true but way 2 is false
beacuse when compare == 0 then v[index-1] release a problem
and we must first check that compare > 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM