Sorting

This is a discussion on Sorting within the C++ Programming forums, part of the General Programming Boards category; ok this program is supposed to perform a merge sort return a vector of class Employee in the order of ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Sorting

    ok this program is supposed to perform a merge sort return a vector of class Employee in the order of lowest salary to highest. I' not sure what's wrong with it. It seems to be fine but my while loop in the main function goes into an infinite loop. Can anyone take a look at it? Thanks for your time.

    Code:
    #include<iostream>
    #include<vector>
    #include<string>
    #include<cstdlib>
    
    using namespace std;
    
    class Employee
    {
    public:
       Employee();
       Employee(string employee_name, double initial_salary);
       double get_salary() const;
       string get_name() const;
    private:
       string name;
       double salary;
    };
    
    Employee::Employee()
    {  
       salary = 0;
    }
    
    Employee::Employee(string employee_name, double initial_salary)
    {  
       name = employee_name;
       salary = initial_salary;
    }
    
    
    double Employee::get_salary() const
    {  
       return salary;
    }
    
    string Employee::get_name() const
    {  
       return name;
    }
    
    
    void merge(vector<Employee*>& a, int from,int mid,int to)
    {
    	int n = to - from + 1;
    	vector<Employee*> b(n);
    
    	int i1 = from;
    	int i2 = mid+1;
    	int j =0;
    
    	while (i1 <=mid && i2<=to)
    	{
    		if(a[i1]->get_salary() < a[i2] ->get_salary())
    		{
    			b[j]->get_name() = a[i1]->get_name();
    			i1 ++;
    		}
    		else
    		{
    			b[j]->get_name() = a[i2]->get_name();
    			i2 ++;
    		}
    		j++;
    	}
    
    	while (i1 <= mid)
    	{
    		b[j]->get_name() = a[i1]->get_name();
    		i1 ++;
    		j++;
    	}
    
    	while (i2 <= to)
    	{
    		b[j]->get_name() =a[i2]->get_name();
    		i2 ++;
    		j++;
    	}
    
    	for (j=0;j<n;j++)
    		a[from + j]->get_name() =b[j]->get_name();
    }
    
    void merge_sort(vector<Employee*>& a,int from,int to)
    {
    	if(from ==to)return;
    	int mid = (from + to)/2;
    	merge_sort(a,from,mid);
    	merge_sort(a,mid+1,to);
    	merge(a,from,mid,to);
    }
    
    void print (vector<Employee*> a)
    {
    	for (int i =0;i<a.size();i++)
    	{
    		cout<<a[i] -> get_name()<<" -> ";
    	}
    }
    
    int main()
    {
    	vector<Employee*> people;
    	bool more;
    	while (more)
    	{
    		cout<<"Enter name, or q to quit:"<<endl;
    		string name;
    		cin>>name;
    		if (name =="q") 
    		{
    			more = false;
    		}
    		else
    		{
    			cout<<"Enter salary for "<<name<<" :"<<endl;
    			double salary;
    			cin>>salary;
    			people.push_back(new Employee(name,salary));
    		}
    	}
    	cout<<"These are the Employees in the Order Inputted:"<<endl;
    	cout<<endl;
    	print(people);
    	merge_sort(people,0,people.size()-1);
    	cout<<"These are the Employees Arranged from the lowest Salary to the Highest: "<<endl;
    	print(people);
    	return 0;
    }
    C++ can hurt.

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Uninitialized variable

    Code:
    bool more;
    maybe
    Code:
    bool more = true;
    Also debug yourīs mergesort algorithm (if you enter 2 or more employies the program crash). Make a search on the board and Iīm pretty sure you will solve your problem (I would help u, but at the moment Iīm in hurry).

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    nono you don't need to put bool more = true at all since whne you initialize a boolean, it's automatically taken as true.

    I fixed the algorithm but now it just doesn't put it in order, it outputs the same thing i input.
    C++ can hurt.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    nono you don't need to put bool more = true at all since whne you initialize a boolean, it's automatically taken as true.
    But you havenīt initialize it!!
    initialized variable
    Code:
    int age = 23;
    uninitialized varibale
    Code:
    int age;
    When I run yourīs program I donīt even have the chance to enter a name which mean that more is not true.
    When you can initialize a variable do it!

    Btw I use Dev-c++
    Last edited by ripper079; 03-25-2003 at 01:35 PM.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    I ran it in VC++ and it crashes after outputting the names....It'll output them in order, but it'll crash, kind of like when your outputting a linked list, and you have a dangling pointer (not pointing to anything)....its too early, or else id look at the code, but right now its just giving me a headache

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 05:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21