Thread: Ordering string by alphabetical order

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Ordering string by alphabetical order

    Code:
    string NameOne = RetrieveName( 1 ); //John
    string NameTwo = RetrieveName( 2 ); //Alisson
    string NameThree = RetrieveName( 3 ); //Alice Cooper
    How would i ordenate strings for alphabetical output? Considering that i have like 600 names to order, i would like the fastest way of doing that. Note: I canīt change the RetrieveName function.

    Thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Add the strings to a vector and then sort it.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    I mean, isnīt it too slow and dont it consume much memory? i have like 600 items to sort and after i have to call
    Code:
    SetNameByIndex( NameTwo, 4 )
    This would take forever (i think)

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    600 names won't probably take more than 1 second.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Just put them in a std::vector and sort them with std::sort.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I mean, isnīt it too slow and dont it consume much memory?
    With only 600 short strings? Maybe if you're using an abacus.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Smile

    hehe thats because im truly concerned about performance in my program hehe im doing a music player for some friends of mine that have truly slow PCīs. And it will be the gamer-friendly music player.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Unless "truly slow" is < 100 MHz than it shouldn't matter. If it's running Windows you should be all right. Why don't you try it? That's the only way to find out. If you don't have access to your friend[']s['] computer[s] then run it on yours, time the result, and divide it by however much faster your computer is than (his|hers|theirs).

    [offtopic] I think "In a world without gods neither masters it is just us and our machines" should be "In a world without gods or masters it is just us and our machines". Just my opinion. [/offtopic]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Scarvenger
    hehe thats because im truly concerned about performance in my program hehe im doing a music player for some friends of mine that have truly slow PCīs. And it will be the gamer-friendly music player.
    Provided you use an O(nlogn) or better algorithm, a computer will be able to sort them in the blink of an eye. std::sort is the way to go.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Provided you use an O(nlogn) or better algorithm
    Actually, with such a small number of items, even the time for a quadratic algorithm wouldn't be noticeable on any machine from the last ten years or so. In fact, if you were writing your own sort, I would recommend something better suited to smaller sets than an O(N logN) algorithm (such as shell sort) because there's less overhead in the actual process. However, std::sort is generally a better choice due to sheer convenience.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Putting a word in alphabetical order.
    By Brewer in forum C Programming
    Replies: 12
    Last Post: 12-16-2006, 05:11 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM