algorithm - Logic challenge: sorting arrays alphabetically in C -
i'm new programming, learning c. i've been working @ problem week now, , can't seem logic straight. straight book i'm using:
build program uses array of strings store following names:
- "florida"
- "oregon"
- "califoria"
- "georgia"
using preceding array of strings, write own
sort()
function display each state's name in alphabetical order usingstrcmp()
function.
so, let's have:
char *statesarray[4] = {"florida", "oregon", "california", "georgia"};
should nested loops, strcmp(string[x], string[y])...
? i've hacked , hacked away. can't wrap head around algorithm required solve efficiently. appreciated!!!
yes, can sort using nested loops. after understand how strcmp() works should straight forward:
strcmp(char *string1, char *string2)
if return value
< 0
indicatesstring1
lessstring2
if return value
> 0
indicatesstring2
lessstring1
- if return value
= 0
indicatesstring1
equalstring2
you can choose of sorting methods once point
this site has ton of great graphical examples of various sorts being performed , includes pseudo code given algorithms.
Comments
Post a Comment