Thread: Fastest way to sort strings?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Fastest way to sort strings?

    what is the best / fastest way to sort strings alphabetically or numerically?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Qsort.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Merge sort can be faster depending on the data given...

  4. #4
    Registered User Ephraim's Avatar
    Join Date
    Feb 2004
    Posts
    8
    ve a look here
    QuickSort

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    i am having trouble implementing quicksort... i cannot find decent implementation details... I'm still pretty green when it comes to programming, so anything in plain english would be most helpful.
    Last edited by criticalerror; 03-04-2004 at 09:44 AM.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    actually, a variation on RADIX applied to strings instead of integers would be much faster than qsort or merge sort.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the best / fastest way to sort strings alphabetically or numerically?
    >I'm still pretty green when it comes to programming
    The best way would be to use the build-in sort function given to you by the standard library:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    void print ( const string& s )
    {
      cout<< s <<endl;
    }
    
    int main()
    {
      string list[] = {
        "This",
        "is",
        "a",
        "test",
        "of",
        "C++'s",
        "string",
        "sorting",
        "capability"
      };
    
      sort ( list, list + 9 );
      for_each ( list, list + 9, print );
    }
    If you have trouble implementing a basic quicksort then why try to use a faster (and more complex) specialized sort? Chances are very good that the general sort function will be more than fast enough.
    My best code is written with the delete key.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it also depends on what you're using it for. If you're trying to store them in a way you can get to them quickly, look up suffox trie (sp?) or hash tables should do the trick. When it comes to finding a string in a list I prefer suffox tries because they are blazing fast.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When it comes to finding a string in a list I prefer suffox tries because they are blazing fast.
    The OP has trouble with quicksort and you suggest using a suffix trie? Have fun explaining how one works and helping with the code.
    My best code is written with the delete key.

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Prelude, he asked what the fastest way was, not which way he or she should use. But you're right of course.

    edit:
    Plus, I haven't really been here the last couple months. I think I'm aloud to be a little bit odd on my return
    Last edited by FillYourBrain; 03-04-2004 at 02:34 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >he asked what the fastest way was, not which way he or she should use
    Actually, he asked for the best/fastest way. The 'best' part gives us a little leway in suggesting what's best for him instead of best in general.

    >I think I'm aloud to be a little bit odd on my return
    That works for me.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fastest way to read / output strings ??
    By author in forum C Programming
    Replies: 3
    Last Post: 06-15-2005, 05:32 AM
  2. Replies: 2
    Last Post: 03-05-2005, 04:00 PM
  3. Sort strings in double linked list. (Optimize code)
    By netstar in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2005, 01:40 AM
  4. bubble sort with strings
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2003, 03:46 PM
  5. Radix Sort, Strings, and Linked Lists
    By dark paladin in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 03:24 PM