Sorting using Max and Min

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    Sorting using Max and Min

    Is it possible to sort 3 integers in either descending/ascending order using ONLY Math.min and Math.max?
    Cannot use the "if", "while", "for", "switch" keywords.
  • dav3
    New Member
    • Nov 2006
    • 94

    #2
    Originally posted by mia023
    Is it possible to sort 3 integers in either descending/ascending order using ONLY Math.min and Math.max?
    Cannot use the "if", "while", "for", "switch" keywords.
    Can you use a list or an array?

    Comment

    • mia023
      New Member
      • May 2007
      • 89

      #3
      Originally posted by dav3
      Can you use a list or an array?
      No but i have to use the command line arguments.

      Comment

      • bravo
        New Member
        • Jul 2007
        • 43

        #4
        May be ternary operator can help u....

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Well the maximum value of x, y and z is max(x, max(y, z)); I sure you can complete
          it from here.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Well the maximum value of x, y and z is max(x, max(y, z)); I sure you can complete
            it from here.

            kind regards,

            Jos
            I expect the problem is determing the middle element.

            Comment

            • rohitchawla
              New Member
              • Jul 2007
              • 85

              #7
              i think middle element can be determined from x,y,z like this
              correct me if i am wrong

              min(min(max(x,y ),max(x,z)),min (max(x,y),max(y ,z)))

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by rohitchawla
                i think middle element can be determined from x,y,z like this
                correct me if i am wrong

                min(min(max(x,y ),max(x,z)),min (max(x,y),max(y ,z)))
                Nah way too complicated; spoiler ahead:







































                x+y+z-min-max is the middle element.

                kind regards,

                Jos

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  Nah way too complicated; spoiler ahead:







































                  x+y+z-min-max is the middle element.

                  kind regards,

                  Jos
                  Kindly proceed to the attic ...

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    Kindly proceed to the attic ...
                    No, I'm afraid of hights; even when I walk on thick socks I get sick.

                    kind regards,

                    Jos ;-)

                    Comment

                    Working...