Ordering Numbers in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bigmac
    New Member
    • Feb 2008
    • 2

    Ordering Numbers in C

    I need to order four numbers and print in ascending order. I'm having troubles coming up with an algorithm to solve this. I should be able to write the code if I got something to work off of. Thanks.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by bigmac
    I need to order four numbers and print in ascending order. I'm having troubles coming up with an algorithm to solve this. I should be able to write the code if I got something to work off of. Thanks.

    If you use an existing library try to use qsort which is part of the C library(stdio/stdlib)


    Raghuram

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      You can also solve this by writing down a comparison tree on your own. For instance, there are only a few possible orderings of 3 elements: A < B < C, A < C < B, B < A < C, B < C < A, C < A < B, and C < B < A. If you compare C to A and find C < A, can the ordering be either the first, second, or third possibility? At this point, you might compare B and A, and come up with 1 or 2 possibilities.

      You can do a similar thing for four elements - I believe it is possible to order four elements using only five comparisons.

      Comment

      • Simonius
        New Member
        • Feb 2008
        • 47

        #4
        First of all, numbers in an array or just 4 integers/doubles/...(you never know).

        If it's an array you have two choises:
        a) a bubble sort
        b) a qsort (which is better)

        A qsort is easier, but if this is for school and you have no experience with declaring functions you might find a bubble sort easier.
        Which one you chose is up to you.
        But I recommend the qsort.

        Comment

        • bigmac
          New Member
          • Feb 2008
          • 2

          #5
          Its still an early intro lvl in C, so i couldnt use any functions and had to use the A<B<C<D ect method. worked out pretty good w/ four ints. thanks all

          Comment

          • Drew Blay
            New Member
            • Aug 2010
            • 1

            #6
            Get a site which includes many math games for free.
            ordering numbers games
            to the main site:
            Online maths games

            Comment

            Working...