Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-11-2003, 11:58 AM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
2d string array sorting....

i cant find any good algoritms to sort and array of strings in alphabetical order. anyone know of a good one that is used for this purpose or at least close, so i can modify it?

thanks

Last edited by GSXdan; 09-11-2003 at 05:50 PM.
GSXdan is offline   Reply With Quote
Old 09-14-2003, 07:28 AM   #2
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
sorting strings is similar to sorting numbers, the only difference is comparing two strings, while you can use the < or > operators with numbers, you can't do so with C strings, some of the options you have:
using comparison functions like strcmp and stricmp, check out cppreference.com or msdn for more info about these functions.
Another option is using the string class that comes with C++ STL (Standard Template Library), again check out cppreference for more info.
As for the sorting algorithm itself, there a lot of algorithms, if you want something simple, use bubble sort, it works fine for small arrays (like 20) if you have larger arrays, you may consider using heap sort or quick sort, I recommend heap sort because it's easier to understand and implement for a new comer to sorting.
Just Google for the sort algorithm name and you'll find info about it, read it, and if you can't understand anything, just post

EDIT:

strcmp:
http://cppreference.com/stdstring_details.html#strcmp

c++ string:
http://cppreference.com/cppstring.html

Bubble sort:
http://linux.wku.edu/~lamonml/algor/sort/bubble.html

Heap sort:
There used to be a great site for heap sort, but I can't manage to find it right now

Last edited by aym; 09-14-2003 at 07:33 AM.
aym is offline   Reply With Quote
Old 09-14-2003, 09:01 AM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
yea i found a modified bubble sort that i came up w/ last year that words just fine for this:

Code:
void Sort(char words[][20], int numwords) {



        int last, next, test;
        char temp[20];

        for(int i=1;i0)
                                {
                                        temp=words[j];
                                        words[j]=words[next];
                                        words[next]=temp;

                                }
                }
        }


        return;


}
thanks again ^dan
GSXdan is offline   Reply With Quote
Old 09-14-2003, 09:04 AM   #4
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
the code isnt showing up correctly for some reason??? if you want to see it i can post the cpp file.

^dan
GSXdan is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:06 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2