Thread: sorting question

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    77

    question

    BK11022
    BS11333
    BK11111
    BH46222




    how do i sort this kind of thing?i treat it as a string?i got try to sort the alphabet thing but the number behide wont be sorted help~!!!
    Last edited by archriku; 03-26-2009 at 11:40 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by archriku View Post
    BK11022
    BS11333
    BK11111
    BH46222




    how do i sort this kind of thing?i treat it as a string?i got try to sort the alphabet thing but the number behide wont be sorted help~!!!
    What order do you want to get after sorting?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Numbers can be sorted as part of a string:

    char numbers[10][10];
    char letters[10][10];

    are both strings if the last char in each entry is the end of string char: '\0'.
    Without that end of string char, each entry would be a collection of char's, but it wouldn't be a string.

    Are you ready for something a little blow your mind ish?

    Usually, you think of sorting as moving things into sorted order, but there is another way to sort things that doesn't move anything except a single number, and I'd recommend it for you, with these structs.

    Why? Because otherwise you have to move every members of each struct that has to be moved, with every swap you make.

    It's called "sorting by Index" and here's how it works:

    You make up a small array of integers, the same size as the array you want to sort.
    Then you set it up with 1,2,3,4... in order, numbers.
    Then you look at your items, and see which one's are out of order, and then you swap - but *not* the items you want sorted - you swap the index number only.

    When you want to see the sorted order, you print or send the output to a file, *using the Index*.

    Code:
    //initialize the Index array:
    for(i = 0; i < MaxSoups; i++)
       Index[i] = i;
    
    /*
    Soup Cans to be Sorted        Index before sorting   Index after sorting
    ========================================================================
    Chicken Soup                           1                    1
    Vegetable Soup                         2                    4 
    Rice with Chicken Soup                 3                    5
    Clear Broth                            4                    3
    Noodles                                5                    2
    */
    
    //Now sort the Soups by Name:
    
    for(i = 0; i < MaxSoups - 1; i++)  {
       for(j = i + 1; j < MaxSoups; j++)  {
          if(strcmp(Soups[i], Soups[j]) > 0)  {
             temp = Index[i];
             Index[i] = Index[j];
             Index[j] = temp;
          }
       }
    }
    
    //Now show the soups in sorted order:
    
    for(i = 0; i < MaxSoups; i++)
       printf("%s \n", Soups[Index[i]]);
    Pretty spiffy, eh?

    Not to worry about the numbers. The telephone book has been sorting numbers for years, and we can, also.

    Vegetable Soup was #2 on the list, and after sorting, it's #5: (of course subtract 1 for C because arrays in C start with zero, not one)
    Last edited by Adak; 03-26-2009 at 04:24 AM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    What order do you want to get after sorting?
    i want to short alphabets 1st in ascending order then number

    BH12345
    BH51234
    BI12345
    BI54321

    hmmm the index thing is too confusing for me oo.. i only learn programming for 2month ... 1 lesson per week...

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by archriku View Post
    i want to short alphabets 1st in ascending order then number

    BH12345
    BH51234
    BI12345
    BI54321

    hmmm the index thing is too confusing for me oo.. i only learn programming for 2month ... 1 lesson per week...
    then just sort them as strings, use strcmp on the whole string including numbered part - and you get what you want
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Numbers sort out lower than letters - and that's how it should be done.


    12345
    99999
    AAAAA
    aaaaa

    Should be your order for the strings shown here. Numbers before letters, Uppercase letters before lowercase.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    f
    Code:
    or(i = 0; i < MaxSoups - 1; i++)  {
       for(j = i + 1; j < MaxSoups; j++)  {
          if(strcmp(Soups[i], Soups[j]) > 0)  {
             temp = Index[i];
             Index[i] = Index[j];
             Index[j] = temp;
          }

    can u please briefly explain what is the temp, i, j variable is? what they represent?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by archriku View Post
    f
    Code:
    or(i = 0; i < MaxSoups - 1; i++)  {
       for(j = i + 1; j < MaxSoups; j++)  {
          if(strcmp(Soups[i], Soups[j]) > 0)  {
             temp = Index[i];
             Index[i] = Index[j];
             Index[j] = temp;
          }

    can u please briefly explain what is the temp, i, j variable is? what they represent?
    What do you think?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    What do you think?

    --
    Mats
    i think i is the number of element of the soup, Maxsoup - 1 becuz array starts with 0, ermm i think j is the second element after i since it is i + 1 so if i is the 1st soup then j is the second soup.. am i correct?but what is temp?i really dun have any idea about this

    oh yea does izzit the Maxsoup must be subtitute by a number?
    Last edited by archriku; 03-26-2009 at 04:55 AM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, i is the index into the array called index - we pass over ALL soups. j is used to work over the "remaining" soups - from the (i+1) to max soups - that way.
    If the condition of strcmp() is true, we have found a soup that is in the wrong order. So we need to swap them.

    Now, imagine that you and a friend is carrying numbers. Numbers are so heavy that you can only hold one. You are standing next to your friend, but the numbers are in the wrong order, so you need to swap with your friend. But neither of you can hold two numbers at once. How are you going to solve it? You need a second friend. Let's call him temp - temp holds your number, you take your friends number, and temp gives your number to your friend. That is how the swap works inside the if-statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    So, i is the index into the array called index - we pass over ALL soups. j is used to work over the "remaining" soups - from the (i+1) to max soups - that way.
    If the condition of strcmp() is true, we have found a soup that is in the wrong order. So we need to swap them.

    Now, imagine that you and a friend is carrying numbers. Numbers are so heavy that you can only hold one. You are standing next to your friend, but the numbers are in the wrong order, so you need to swap with your friend. But neither of you can hold two numbers at once. How are you going to solve it? You need a second friend. Let's call him temp - temp holds your number, you take your friends number, and temp gives your number to your friend. That is how the swap works inside the if-statement.

    --
    Mats

    ic

    so lets have i have 4 matrix number BK12345 BK54321 BX12345 BX54321

    i declare them as
    Code:
     char matrix[10], temp;
    so i do my sorting code lidis?

    Code:
    for ( i = 0; i < 4; i++);
    for( j = i + 1 ; j < 5; j++); {
    if(strcpm(matrix[i],matrix j)>0) {
    temp = matrix[i]
    matrix[i] = matrix[j]
    matrix[j] = temp
    }
    but then Adak said the BK123123 in 2D array matrix[10][10]

    so how i write for the matrix[i]?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A string "BK12345" is indeed a character array in itself, and you could not fit that in a single 10 character array like you declare in your post.

    Also, for copying strings, you need to use strcpy(), otherwise it won't work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    A string "BK12345" is indeed a character array in itself, and you could not fit that in a single 10 character array like you declare in your post.

    Also, for copying strings, you need to use strcpy(), otherwise it won't work.

    --
    Mats
    so if it can fit then i declare it ass Matrix[10][10]

    then how do i do the sorting? matrix[i][?]

    sorry i m very very new to programming..

    strcpy like that?

    strcpy(temp, matrix[i]);
    strcpy(matrix[i], matrix[j]);
    strcpy(matrix[j], temp);

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, your strcpy() looks right. Of course, temp needs to be the same size as the second size in matrix.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    Yes, your strcpy() looks right. Of course, temp needs to be the same size as the second size in matrix.

    --
    Mats
    but then can u answer me how i do the sorting for matrix[10][10]?

    cuz what i can do is single array matrix[i] but for matrix[i][?]<<< what i do with the second array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Sorting Question
    By Rkukulski in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 01:37 PM
  3. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. an actual question (sorting arrays)
    By master5001 in forum C Programming
    Replies: 4
    Last Post: 08-13-2001, 10:21 PM