Thread: need help by "sorting an array of string"

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Question need help by "sorting an array of string"

    hi ...
    i'm trying to sort an array of string.
    my code should sort my array alphabetically [a-z] ,but it dos not do that ..

    Code:
    int main(void)
    
    {
    char mov[7][25];
    int f,c,i; ///first , current , i-->char index 0-24;
    int tmp,save[7]={0,1,2,3,4,5,6}; 
    i=23;
    
    strcpy(mov[0],"Cheaper by the Dozen 2");
    strcpy(mov[1],"Drumline");
    strcpy(mov[2],"Ever After");
    strcpy(mov[3],"Cheaper by the Dozen");
    strcpy(mov[4],"Mr. & Mrs. Smith");
    strcpy(mov[5],"X-Men");
    strcpy(mov[6],"Toy Story 2");
     
    
    puts("<----before---->");
     for(i=0;i<7;i++)puts(mov[save[i]]);
    puts("<--------------->"); 
    
    
     while(i>=0)
    {
    for(f=0;f<6;f++)
    for(c=f+1;c<7;c++)
    	{
    	if(mov[f][i]<mov[c][i])
    		{
    		tmp=save[f];
    		save[f]=save[c];
    		save[c]=tmp;
    		}
    	}
    i--;
    }
    puts("<-----after----->");
     for(i=0;i<7;i++)puts(mov[save[i]]);
    puts("<--------------->");
    return 0;
    }
    output :

    <----before---->
    Cheaper by the Dozen 2
    Drumline
    Ever After
    Cheaper by the Dozen
    Mr. & Mrs. Smith
    X-Men
    Toy Story 2
    <--------------->
    <-----after----->
    Cheaper by the Dozen 2
    Drumline
    Mr. & Mrs. Smith
    Ever After
    X-Men
    Cheaper by the Dozen
    Toy Story 2
    <--------------->

    as you see in the output the string is not sorted .. i think, I'm missing something basic here...

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    The order of your loops is jumbled up. I am not going to give you the code, but write some pseudocode which you should be able to follow

    Code:
    for i in range(0, numberofstrings)
     for j in range(0, numberofstrings-i-1)
      string1=arr[j]
      string2=arr[j+1]
      for k in range(0, min(size(string1), size(string2)))
       if string1[k] > string2[k]
        swap string1 and string2 and break
       else if string1[k] < string2[k]
        break
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Look at the way you're comparing items. For example, take just these two strings:
    Code:
    Drumline
    Ever After
    First you're comparing the first chars D and E and you make Drumline come first because D is less than E.
    Then you're comparing the second chars r and v and again you make Drumline come first because r is less than v.
    Then you're comparing the third chars u and e and now you make Ever After come first because e is less than u - WTF!

    That's not how you order strings. Once you're compared the first characters and they are not the same then the rest of the characters are not relevant.
    Of course that isn't the entire extent of the problems with that code, by far.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh no wait, that outer loop is going backwards, and from only 7. It's actually not doing what I just said - It's far worse!

    This code is stretching the limits of just how wrongly something can be done...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM