954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sorting a text file

Hi guys,

If i have a file like the one below what would be the best/easiest way to sort the words in alphabetical order. thanks.

with
an
equal
mix
of
biosolids
to
make
the
compost
Sawdust
is
on
their
greens
Compost
applied
properly
will
enhance
growth
of
grass
flowers
shrubs
by
improving
the

hariza
Light Poster
38 posts since Sep 2006
Reputation Points: 31
Solved Threads: 0
 

c or c++? read all the words into an array of strings then use either C's qsort() function or c++ sort(). If you are required to write your own sort routine then you can't use either of those functions. in that case, google for "sort algorithms" and you will find quite a few of them. Bubble Sort is the easiest to code but can be the slowest to execute.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

If you need to write sort code, I would recommend insertion sort.
Read this:
http://www.eternallyconfuzzled.com/tuts/sorting.html

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 
If you need to write sort code, I would recommend insertion sort. Read this: http://www.eternallyconfuzzled.com/tuts/sorting.html


Not me. I'd use a bubble sort. The list is short. The sort is easy. Start with the easiest and when comfortable, move up to the harder ones.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

I disagree. Many people thinks bubble sort is easiest just because it is usually first sort algorithm teached in many books and classrooms. It's performance are bad for large lists and it's much better in my opinion to get used to better algorithms. Insertion sort is more natural and i don't see why it is harder to understand comparing with bubble sort.

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

But still given that the worst sorting time for "Quicksort" equals the time complexity of bubble sort, so it does no harm in using the fastest algorithm out there, just to keep the algo common independent of the data fed into it.

The below link is interesting to look at :
http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/sortcontest/sortcontest.htm

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

If you use C++, load a string vector and use the vector's sort. Then you don't have to give a rat-poop about the sorting algorithm, it will be the best! A somewhat modern example:

// load a string vector with words from a whitespace 
// separated text file and sort the words

/* typical sample file:
with
an
equal
mix
of
biosolids
to
make
the
compost
*/

#include <string>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main() 
{
  vector<string> sV;
  ifstream in("compost.txt");
  string word;
  
  // separates words in file at a whitespace
  // and loads a string vector
  while(in >> word)
    sV.push_back(word);
     
  cout << "Unsorted words:" << endl;
  for(int i = 0; i < sV.size(); i++)
    cout << sV[i] << endl;
  
  cout << "---------------" << endl;
    
  cout << "Sorted words:" << endl;
  sort( sV.begin(), sV.end() );
  for(int i = 0; i < sV.size(); i++)
    cout << sV[i] << endl;  
  
  cin.get();
  return 0;
}
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Yeah i agree with Mr. Ene Uran. The inbuilt vector sort function is much more sophisticated compared to the normal sort functions in terms of both performance and interface.

The STL implementation of sort, however, uses the introsort algorithm whose worst case complexity is O(N log(N)). Introsort is very similar to median-of-three quicksort, and is at least as fast as quicksort on average.

Look here: http://www.sgi.com/tech/stl/sort.html

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
If i have a file like the one below what would be the best/easiest way to sort the words in alphabetical order.


Whatever way the system/compiler offers. :) If you just need the file sorted beforehand, I don't know of an OS that doesn't give you an easy to use sort program from the command prompt. If you need to sort the file at runtime from your program, both C and C++ have good and easy to use sorting functions.Not me. I'd use a bubble sort. The list is short. The sort is easy.
The list was an example, I think. hariza said the file was like the one pasted, so it could be a complete dictionary. Bubble sort would choke on somethin' like that. :) I'd prolly use Shell sort as a first draft, then somethin' harder if it's not good enough.

Inanna
Junior Poster in Training
90 posts since Oct 2006
Reputation Points: 53
Solved Threads: 6
 

For something like a dictionary sized array of strings I would use a combination of sorting routines with look-ahead. Something Narue's snippet "Optimized quicksort implementation" shows:
http://www.daniweb.com/code/snippet61.html

I did a comparison of simple sorting routines and came up with this:
Sorting an integer array (50000 random integers) -->
qsort() = 31ms shellsort = 922ms insertionsort = 2672ms bubblesort = 12032ms
see: http://www.daniweb.com/code/snippet377.html

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You