By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,169 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,169 IT Pros & Developers. It's quick & easy.

how can i sort words in c?

P: 3
given a linked list of words, how can i sort them out alphabetically in c? pls help me... i'm really having a hard time in this problem..
Mar 13 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
You will have to use strcmp(char* str1, char* str2), which returns 1 if str1 is greater than str2, -1 if str1 is less than str2, and 0 if they are equal. For the actual sorting, there are plenty of algorithms that you can find online, such as the Bubble Sort, the Quicksort, or the Insertion sort algorithms. You will have to implement these algorithms yourself, though.
Mar 13 '07 #2

P: 39
I would recommend using qsort just because it is already developed and enhanced and will meet most needs. Plus you won't have to create anything crazy or complicated.
Mar 13 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
I would recommend using qsort just because it is already developed and enhanced and will meet most needs. Plus you won't have to create anything crazy or complicated.
Unfortunately qsort works on an array not a linked list.

For linked lists another method is rather than using a sorting algorithm just to recreate the list in the desired order

Expand|Select|Wrap|Line Numbers
  1. PSEUDOCODE
  2. HEAD OF SortedList =HEAD OF UnsortedList
  3. REMOVE HeadOfUnsortedList
  4.  
  5. WHILE UnsortedList NOT EMPTY
  6.    NextEntryToInsert = HEAD OF UnsortedList
  7.    REMOVE HeadOfUnsortedList
  8.  
  9.    SCAN DOWN SORTED LIST FOR POSITION TO INSERT NextEntryToInsert
  10.    INSERT NextEntryToInsert
  11. WEND
  12.  
Mar 13 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.