
-----------------------------------
Jeevan25
Wed Dec 21, 2005 5:09 pm

string sort
-----------------------------------
i am in need of help. i am a high school student taking c++. i am using dev c++ as my ide. i need to write a program that reades from a file. the file contains 1000 words in random order. one word per line. the program reads the file and sort the words in alphabetic order. then it outputs the sorted words to a new file. for example. 
unsorted words.datpile
game
they
apple

now the program will read this file and sort the words in alphabetic order and save to a new file.
sorted words.dat apple
game
pile
they how do i do this? pls help. thanks.

-----------------------------------
wtd
Wed Dec 21, 2005 5:13 pm


-----------------------------------
Well, first you need to read the lines from the file into something like a:

std::vector

Now, here's where you ask me what that is.  Don't worry, specific questions are how you learn.  :)

-----------------------------------
Jeevan25
Wed Dec 21, 2005 7:42 pm


-----------------------------------
yeap. i didnt go that far. my teacher told me three ways. select sort, insertion sort and bubble sort. i didn't learn what you typed. :D my teacher ain't that great either. she tells me to come after school but she is never there.

-----------------------------------
wtd
Wed Dec 21, 2005 7:44 pm


-----------------------------------
Well, as I said, the first step is to get the lines into some kindof collection, then you can worry about the sort.

-----------------------------------
Martin
Wed Dec 21, 2005 7:56 pm


-----------------------------------
File input and output:
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html

How to use Vectors and arrays:
http://cplus.about.com/od/beginnerctutorial/l/aa050102a.htm

And finally, sorting. If you haven't heard of any of these, start with the bubble sort. It's horribly slow, but very easy to do and for 1000 entries will be more than fast enough.
http://en.wikipedia.org/wiki/Sort_algorithm

Your program will look something like this (assuming you're going for a functional (ie. non object oriented) approach):

1. Create a new vector.
2. Open the file.
3. Loop through the file and fill up the vector with the file's contents
4. Close the file.
5. Sort the contents of the vector.
6. Output results.

-----------------------------------
Jeevan25
Wed Dec 21, 2005 10:03 pm


-----------------------------------
i need to know the loops and the if statements to sort the words. i know how to read and write to files.

-----------------------------------
bugzpodder
Wed Dec 21, 2005 10:37 pm


-----------------------------------
if you find the examples martin give are not enough, then I suggest you go speak to your teacher.  He will be more accessible and provide more relevant help

-----------------------------------
Jeevan25
Wed Dec 21, 2005 10:46 pm


-----------------------------------
yeah right. i hate her. i ask for help with my code. somthing is wrong. she ask like 3 people who are getting moer than 80 to help me because she couldn' find problem. then she called the best student. he is the one who solved it. not the teacher. if there was a worst teacher of millenium she would be that one.

-----------------------------------
wtd
Wed Dec 21, 2005 10:51 pm


-----------------------------------
Shame the STL can't be fully utilized.

#include 
#include 
#include 
#include 
#include 

using namspace std;

int main()
{
   vector words;
   ifstream input_file("file_name");

   copy(istream_iterator(input_file), 
        istream_iterator(),
        back_inserter(words));   
   sort(words);
   copy(words.begin(), 
        words.end(), 
        ostream_iterator(cout, "\n"));

   return 0;
}

-----------------------------------
wtd
Wed Dec 21, 2005 11:22 pm


-----------------------------------
Hmmm... actually...

#include 
#include 
#include 
#include 
#include 

using namspace std;

int main()
{
   ifstream input_file("file_name");
   vector words(istream_iterator(input_file),
                        istream_iterator());

   sort(words);
   copy(words.begin(), 
        words.end(), 
        ostream_iterator(cout, "\n"));

   return 0;
}

-----------------------------------
MysticVegeta
Sun Dec 25, 2005 12:59 pm


-----------------------------------
haha I love this sort commands which are not there in Turing by the ways, But I have a question, Didn't your teacher say about "shell sort"? I think thats the fastest way to sort... 

http://linux.wku.edu/~lamonml/algor/sort/sort.html

-----------------------------------
Hikaru79
Sun Dec 25, 2005 2:59 pm


-----------------------------------
Didn't your teacher say about "shell sort"? I think thats the fastest way to sort... 

Haha, no, it's the fastest of the O(n^2) algorithms -- that's far, far away from being faster than quicksort or merge sort (for anything other than trivially-sized lists, that is).

-----------------------------------
wtd
Sun Dec 25, 2005 3:01 pm


-----------------------------------
http://en.wikipedia.org/wiki/Sorting_Algorithm

-----------------------------------
MysticVegeta
Mon Jan 02, 2006 12:06 am


-----------------------------------
Heap sort seems to beat merge and quicksort..

-----------------------------------
Geminias
Mon Jan 02, 2006 10:02 am


-----------------------------------
#include 
#include 
#include 
#include 
#include 

using namspace std;  //typo should be "namespace"

int main()
{
   ifstream input_file("file_name");
   vector words(istream_iterator(input_file),
                        istream_iterator());

   sort(words);
   copy(words.begin(),
        words.end(),
        ostream_iterator(cout, "\n"));

   return 0;
} 

if namespace were spelled right would this code compile for you wtd?

-----------------------------------
wtd
Mon Jan 02, 2006 2:08 pm


-----------------------------------
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std; //typo should be "namespace"

int main()
{
   ifstream input_file("file_name");
   vector words =
      vector(istream_iterator(input_file),
                     istream_iterator());

   sort(words.begin(), words.end());
   copy(words.begin(),
        words.end(),
        ostream_iterator(cout, "\n"));

   return 0;
}

Now it compiles.

-----------------------------------
Geminias
Tue Jan 03, 2006 12:36 am


-----------------------------------
oh i see, you left out an equal sign too.. thats why i was getting all these errors.  

I like the vector class, its cool.  

One question though.. what is istream_iterator?

-----------------------------------
wtd
Tue Jan 03, 2006 12:53 am


-----------------------------------
The entire Standard Template Library very heavily uses iterators.  It essentially is a value which represents a position in a collection.

In this case we're dealing with an input stream iterator.

istream_iterator(input_file)

Creates a new iterator pointing to the beginning of the input_file input stream.  

istream_iterator()

Represents the end of the stream.

-----------------------------------
Geminias
Tue Jan 03, 2006 1:07 am


-----------------------------------
thanks.. it has become more clear except how do iterators differ from pointers?  

and why do they use the underscore convention?

 So "istream_iterator " is an overloaded function and when you pass it no parameter it defaults to pointing to the end of a istream?

-----------------------------------
wtd
Tue Jan 03, 2006 1:17 am


-----------------------------------
thanks.. it has become more clear except how do iterators differ from pointers?  

In a lot of ways they can be used very similarly to pointers.

and why do they use the underscore convention?

Why not?  :)

C++ forces no particular naming conventions, so it's pretty much a matter of taste.  Unfortunately, lots of different libraries do things differently, so you can't really justify the use of any one convention as a means of maintaining consistency with the names from libraries.

I guess the programmers at SGI decided they liked underscores.  Perhaps they also know that lowercase letters are typically easier to read than capitals, and without camelCasing, you need underscores as separators.

So "istream_iterator " is an overloaded function and when you pass it no parameter it defaults to pointing to the end of a istream?

istream_iterator is a class, actually.  When you pass it no argument, you get the equivalent of a NULL pointer.  Then when the STL function using the iterator is looping it basically starts with the first iterator can advances it until it equals the second iterator.  

At the end of the input stream, it gets that NULL equivalent, which equals the second iterator, and that tells it to stop iterating (looping).

-----------------------------------
Geminias
Tue Jan 03, 2006 2:48 am


-----------------------------------
the reason i asked about the underscore was because i thought iterator was a member function of istream.  i didnt realize istream_iterator was a class.  I pretty much understand now. TY :wink:

-----------------------------------
md
Tue Jan 03, 2006 10:21 pm


-----------------------------------
Heap sort seems to beat merge and quicksort..
Erm... wha? Methinks you're mistaken... Quick sort and Merge sort are the two fastest sorts given most sets of data. Heap sort is fast too, and is the same O(n log n), but in practice it ends up being slower then quick sort or merge sort.

-----------------------------------
DIIST
Tue Jan 03, 2006 10:55 pm


-----------------------------------
Merge, & Quick sort may be fast. But they are hard to understand. Well for me any way. Bubble Sort and the minor sorts seem to make sence.  :roll:

-----------------------------------
wtd
Tue Jan 03, 2006 11:37 pm


-----------------------------------
the reason i asked about the underscore was because i thought iterator was a member function of istream.  i didnt realize istream_iterator was a class.  I pretty much understand now. TY :wink:

http://www.sgi.com/tech/stl/

-----------------------------------
md
Wed Jan 04, 2006 12:17 am


-----------------------------------
Merge, & Quick sort may be fast. But they are hard to understand. Well for me any way. Bubble Sort and the minor sorts seem to make sence.  :roll:
Bubble sort is a great thing to start off with precisely because it _is_ easy to understand. But once you get the hang of it learning how quick sort and merge sort work is a good idea.

-----------------------------------
DIIST
Wed Jan 04, 2006 11:29 am


-----------------------------------

Bubble sort is a great thing to start off with precisely because it _is_ easy to understand. But once you get the hang of it learning how quick sort and merge sort work is a good idea.

I wonder if there is like a tutorial that explains the different sorting algorithms. I personally learned bubble sort and the minor sorts form google sites. Its only shell sort and up, that i dont get. :?

Shell sort uses 2d arrays though so i guess you have to go 3d, if you are dealing with sorting strings.  :?:

-----------------------------------
Geminias
Wed Jan 04, 2006 6:36 pm


-----------------------------------
you wouldn't "have" to go 3d arrays.  its absolutely no different except in terms of organization.  so if you thought it would be easier you could do a 1d array  :lol:
