Thread: Simple sort

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Simple sort

    Okay I've been running through some ideas on how to sort a list of names in alphabetical order, all names are like Smith. 1st letter is uppercase the rest are lowercase.
    I was thinking about somehow converting the strings into a number then sorting it be numerical value then reoutputting the name but that didn't work.

    My plan for example if I had five names:
    Abe
    Dan
    Albert
    Smith
    Randall

    Abe and Dan are alright so I then compare dan and albert but they are wrong so I swap them, and smith and randall are screwed up so swap them:
    Abe
    Albert
    Dan
    Randall
    Smith
    So now they are sorted.

    This is generally what I have but it doesn't seem to work..
    input 1st name
    loop till sorted
    input 2nd name
    loop till end of file is reached
    if the two names are sorted continue
    if the two names are not
    swap them


    Code:
    inName >> name1;
    	
    	while (!inName.eof())
    	{
    		inName >> name2;
    		if(name1 < name2)
    		{
    			outName << name1 << endl;
    		}
    		if(name1 > name2)
    		{
    			temp = name1;
    			name1 = name2;
    			name2 = temp;	
    			outName << name1 << endl << name2 << endl;
    		}	
    	}
    Thats all I have I can't seem to get it finished.. Any help would be appreciated.. outName is an ofstream variable to output to a file. inName inputs from a file .... I'm not using arrays or any sort functions. Basic stuff I guess is what you call it. heh
    Last edited by StarOrbs; 03-28-2005 at 04:29 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea is sort() from the STL library.
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
       vector<string> names;
       string filename = "names.txt";
       
       ifstream inName(filename.c_str());
       if (!inName.is_open())
       {
          cout << "Unable to open file: " << filename << endl;
          return 1;
       }
       
       string name;
       while (inName >> name)
          names.push_back(name);
    
       for (int i=0; i<names.size(); i++)
          cout << names[i] << endl;
    
       sort(names.begin(),names.end());
    
       cout << endl << "Names in alphabetical order" << endl;
       for (int i=0; i<names.size(); i++)
          cout << names[i] << endl;
    
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Unless the names are almost in order, I think you are going to have to use an array or vector of some sort.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Well I have two data files which are sorted name1 and name2. Then I have to output them into one file name sorted. No arrays or vectors yet at least with this problem or the sort(). We have only had <string> <iostream> <fstream> <iomanip>.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Any Ideas? I have two files data1 and data2 which then need to be combined into one file sorted in alphabetical order. Perhaps that was a better way of saying it. I spent about 3 more hours last night to no avail.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Unless the original files are sorted, you can't sort them without first saving into an array.

    IF the original files are sorted then you could do something like:
    Code:
    read in name1
    read in name2
    while either file is not empty
       if name1<name2 or 2nd file EOF output name1 and load new name1
       else if name2<name1 or 1st file EOF output name2 and load new name2
    If they aren't sorted, you HAVE to load both files completely into an array/vector before you can sort.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    The problem with that is as soon as there is a word not in alphabetical order in just keeps repeating the word until infinity. I tried to alter it so it swaps the words but it still repeats.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You talking about my pseudocode? Like I said, if the two files aren't alphabetical to begin with, my code won't work. If they are, post your code and I'll see if I can spot the problem.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Code:
    /* 
    	
    	2. Ordered List
    	Description: Program that will combine to lists of data and put them in a sorted order.
    	File: main.cxx
    	Programmer: ****
    	E-mail: ****
    	Compiler: g++
    	Last modified: 3/29/05
    */
    using namespace std;
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main()
    {
    	
    	string namesOne, // Used to temporarily store a name from the first file
    		   namesTwo, // Used to temporarily store a name from the second file
    		   temp; // Used to swap the two variables
    	
    	ifstream inNames1, // Used to input the names from the first file
    			 inNames2; // Used to input the names from the second file
    	ofstream outNames; // Used to output names to a file
    	
    	inNames1.open("/Users/****/Documents/School/Rutgers/CS/P4_Luebcke_HOME/merge/names1.data");
    	inNames2.open("/Users/****/Documents/School/Rutgers/CS/P4_Luebcke_HOME/merge/names2.data");
    	outNames.open("/Users/****/Documents/School/Rutgers/CS/P4_Luebcke_HOME/merge/names.data");
    	
    	inNames1 >> namesOne;
    	inNames2 >> namesTwo;
    	
    	while(!inNames1 || !inNames2)
    	{
    		if(namesOne < namesTwo || !inNames2.eof())
    		{
    			outNames << namesOne << endl;
    			inNames1 >> namesOne;
    		}
    		else if(namesTwo < namesOne || !inNames1.eof())
    		{
    			outNames << namesTwo << endl;
    			inNames2 >> namesTwo;
    			temp = namesTwo;
    			namesTwo = namesOne;
    			namesOne = temp;
    		}
    		
    	}
    	
    	inNames1.close();
    	inNames2.close();
    	outNames.close();
    	return 0;
    }

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok, i think you are confused on how to tell if a file is EOF. I admit I'm not well versed in file manipulation so someone could easily correct me, but I think eof() returns true when the end is reached, so both
    Code:
    if(namesOne < namesTwo || !inNames2.eof())
    statements need to be changed to
    Code:
    if(namesOne < namesTwo || inNames2.eof())
    Also, I'm not sure if "!inNames1" works the way you expect it to. Change that line to use eof's as well:
    Code:
    while(!inNames1.eof() || !inNames2.eof())
    Also, if you notice, when file1 is eof you are still comparing namesOne to namesTwo, BUT namesOne still has an old name that you never cleared out. If that old name is less than the current namesTwo you'll get an infinite loop. Also be sure to take out the swap you placed in there before retesting.

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Great, I can't believe all that trouble because of a ! hehe... Now I just have to figure out why it doesn't input the last two names in both the files.. Thanks a lot for your help PJYelton.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Sort two vectors in the same order
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2008, 05:49 PM
  3. choosing right sort algorithm
    By Micko in forum C++ Programming
    Replies: 15
    Last Post: 05-29-2006, 10:38 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM