Thread: Sort 3 letter by ASCII

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    18

    Sort 3 letter by ASCII

    Hi guys, I am total beginner. I have trouble with my first school work in programming. I have no idea how to sort 3 integers by ASCII ( Lexicographic).
    For Example: C A B = A B C.

    I cant use cycles, bubble sort and field.

    I try some codes, but nothing worked.



    Can you give me some help? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should post one of your better attempts, then we can guide you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I think the issue is that he's not allowed to use loops (cycles), which would exclude most sorts, leaving something like a sorting network, which is just a series of if statements and swaps. For 3 elements, 3 if / swap statements are needed. On a side note, 4 elements need 5 if / swap statements, and 10 elements would need 29 if / swap statements. Sorting network is one of the lesser known sorting methods, and only useful for sorting a small and fixed number of elements, sometimes implemented in hardware.

    So the idea here is for Jamesss to figure out the 3 if / swap statement needed to sort 3 integers (the ASCII part doesn't really affect how this is done).

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    I try this but this is probably very bad Doesnt work.

    Code:
    int main(){
    printf("Lexicograpgic:\n");
    int  a,b,c;
    
    
       scanf("%c",&a,&b,&c);
    
    
        {if (a < b)
            {if (a < c)
                {if (b < c)
                    printf("\n",&a,&b,&c);
                else
                    printf("\n",&a,&c,&b);
                }
    
    
    
    
            else
                printf("\n",&c,&a,&b);
         }
       else
       {if (b < c)
             {if (a < c)
                printf("\n",&b,&a,&c);
             else
                printf("\n",&b,&c,&a);
       }
          else
             printf("\n",&c,&b,&a);
          }}
    
    
    
    
        return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well your logic looks fine (or at least plausible).

    Your use of printf and scanf however needs lots of work.

    Eg
    scanf("%d %d %d", &a, &b, &c);

    and
    printf("%d %d %d\n", a, b, c );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by Salem View Post
    Well your logic looks fine (or at least plausible).

    Your use of printf and scanf however needs lots of work.

    Eg
    scanf("%d %d %d", &a, &b, &c);

    and
    printf("%d %d %d\n", a, b, c );

    I do this but still not work, program write numbers.

    Code:
    int main(){
    printf("Lexicograpgic:\n");
    int  a,b,c;
    
    
       scanf("%d,%d,%d",&a,&b,&c);
    
    
        {if (a < b)
            {if (a < c)
                {if (b < c)
                    printf("%d,%d,%d\n",a,b,c);
                else
                    printf("%d,%d,%d\n",a,c,b);
                }
    
    
    
    
            else
                printf("%d,%d,%d\n",c,a,b);
         }
       else
       {if (b < c)
             {if (a < c)
                printf("%d,%d,%d\n",b,a,c);
             else
                printf("%d,%d,%d\n",b,c,a);
       }
          else
             printf("%d,%d,%d\n",c,b,a);
          }}
    
    
    
    
        return 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I thought you understood.

    You have to type in the commas to match your format string.

    Code:
    $ ./a.out 
    Lexicograpgic:
    6 2 4
    0,0,6
    
    $ ./a.out 
    Lexicograpgic:
    6,2,4
    2,4,6
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by Salem View Post
    I thought you understood.

    You have to type in the commas to match your format string.

    Code:
    $ ./a.out 
    Lexicograpgic:
    6 2 4
    0,0,6
    
    $ ./a.out 
    Lexicograpgic:
    6,2,4
    2,4,6
    Ok I have this and it work so thank you, but it work only with numbers and I need letters a-z.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The logic is ok, just needed to fix scanf and printf lines. This example uses spaces instead of commas for both input and output. Enter characters instead of numbers:

    Code:
    int main()
    {
    char a,b,c;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c",&a,&b,&c);
        if (a < b){
            if (a < c){
                if (b < c)
                    printf("%c %c %c\n",a,b,c);
                else
                    printf("%c %c %c\n",a,c,b);
            } else
                printf("%c %c %c\n",c,a,b);
        } else {
            if (b < c){
                if (a < c)
                    printf("%c %c %c\n",b,a,c);
                else
                    printf("%c %c %c\n",b,c,a);
            } else
                printf("%c %c %c\n",c,b,a);
        }
        return 0;
    }
    Example runs with all 6 cases:

    Lexicograpgic:
    a b c
    a b c

    Lexicograpgic:
    a c b
    a b c

    Lexicograpgic:
    b a c
    a b c

    Lexicograpgic:
    b c a
    a b c

    Lexicograpgic:
    c a b
    a b c

    Lexicograpgic:
    c b a
    a b c
    Last edited by rcgldr; 10-17-2015 at 04:59 AM.

  10. #10
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by rcgldr View Post
    The logic is ok, just needed to fix scanf and printf lines. This example uses spaces instead of commas for both input and output. Enter characters instead of numbers:

    Code:
    int main()
    {
    char a,b,c;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c",&a,&b,&c);
        if (a < b){
            if (a < c){
                if (b < c)
                    printf("%c %c %c\n",a,b,c);
                else
                    printf("%c %c %c\n",a,c,b);
            } else
                printf("%c %c %c\n",c,a,b);
        } else {
            if (b < c){
                if (a < c)
                    printf("%c %c %c\n",b,a,c);
                else
                    printf("%c %c %c\n",b,c,a);
            } else
                printf("%c %c %c\n",c,b,a);
        }
        return 0;
    }
    Example runs with all 6 cases:

    Lexicograpgic:
    a b c
    a b c

    Lexicograpgic:
    a c b
    a b c

    Lexicograpgic:
    b a c
    a b c

    Lexicograpgic:
    b c a
    a b c

    Lexicograpgic:
    c a b
    a b c

    Lexicograpgic:
    c b a
    a b c

    Oooo it works! I forgot change int fot char. Thank you mate!

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Sorting network version. I'm assuming that no fields means you can't use temp variable, so I used xor swap method:

    Code:
    int main()
    {
    char a,b,c;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c",&a,&b,&c);
        if(a > c){a ^= c; c ^= a; a ^= c;}
        if(a > b){a ^= b; b ^= a; a ^= b;}
        if(b > c){b ^= c; c ^= b; b ^= c;}
        printf("%c %c %c\n",a,b,c);
        return 0;
    }
    For 3 letter case, it's not saving much, but for 4 letter case, only 5 if / swap statements are needed to handle all 24 permutations.

    Code:
    int main()
    {
    char a,b,c,d;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c %c",&a,&b,&c,&d);
        if(a > c){a ^= c; c ^= a; a ^= c;}
        if(b > d){b ^= d; d ^= b; b ^= d;}
        if(a > b){a ^= b; b ^= a; a ^= b;}
        if(c > d){c ^= d; d ^= c; c ^= d;}
        if(b > c){b ^= c; c ^= b; b ^= c;}
        printf("%c %c %c %c\n",a,b,c,d);
        return 0;
    }
    For 10 letter case, 29 if / swap statements would be needed to handle all 3,628,800 permutations.

  12. #12
    Registered User
    Join Date
    Oct 2015
    Posts
    18
    Quote Originally Posted by rcgldr View Post
    Sorting network version. I'm assuming that no fields means you can't use temp variable, so I used xor swap method:

    Code:
    int main()
    {
    char a,b,c;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c",&a,&b,&c);
        if(a > c){a ^= c; c ^= a; a ^= c;}
        if(a > b){a ^= b; b ^= a; a ^= b;}
        if(b > c){b ^= c; c ^= b; b ^= c;}
        printf("%c %c %c\n",a,b,c);
        return 0;
    }
    For 3 letter case, it's not saving much, but for 4 letter case, only 5 if / swap statements are needed to handle all 24 permutations.

    Code:
    int main()
    {
    char a,b,c,d;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c %c",&a,&b,&c,&d);
        if(a > c){a ^= c; c ^= a; a ^= c;}
        if(b > d){b ^= d; d ^= b; b ^= d;}
        if(a > b){a ^= b; b ^= a; a ^= b;}
        if(c > d){c ^= d; d ^= c; c ^= d;}
        if(b > c){b ^= c; c ^= b; b ^= c;}
        printf("%c %c %c %c\n",a,b,c,d);
        return 0;
    }
    For 10 letter case, 29 if / swap statements would be needed to handle all 3,628,800 permutations.
    Can I ask you what mean " ^= " ? Your code look better , but this is not clear for me
    Code:
     {a ^= c; c ^= a; a ^= c;}
    etc.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jamesss
    Can I ask you what mean " ^= " ?
    It is exclusive or compound assignment. If you have not learnt this, then perhaps it is not required and you should use an additional variable to perform the swap.

    You probably should clarify what you mean by "cycles" and "field".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Using a temp variable t to do swaps for the 3 letter case:

    Code:
    int main()
    {
    char a,b,c,t;
        printf("Lexicograpgic:\n");
        scanf("%c %c %c",&a,&b,&c);
        if(a > c){t = a; a = c; c = t;}
        if(a > b){t = a; a = b; b = t;}
        if(b > c){t = b; b = c; c = t;}
        printf("%c %c %c\n",a,b,c);
        return 0;
    }

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    As for the XOR swap, note that x^x == 0.

    Code:
        a ^= b;   // a = (a^b)
        b ^= a;   // b = (a^b)^b = a^(b^b) = a^0 = a
        a ^= b;   // a = (a^b)^a = (a^a)^b = 0^b = b

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Radix Sort ( + counting sort) does not stable sort.
    By siepet in forum C Programming
    Replies: 6
    Last Post: 11-26-2013, 12:04 PM
  2. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  3. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM

Tags for this Thread