Sort Three Numbers

Problem Statement

Give three integers, display them in ascending order. For example, if the input is 2, 3 and 1, this program should display 1, 2 and 3.

Solution

! -------------------------------------------------------
! This program reads in three INTEGERs and displays them
! in ascending order.
! -------------------------------------------------------

PROGRAM  Order
   IMPLICIT  NONE

   INTEGER  :: a, b, c

   READ(*,*)  a, b, c

   IF (a < b) THEN                 ! a < b here
      IF (a < c) THEN              !   a < c     : a the smallest
         IF (b < c) THEN           !      b < c  : a < b < c
            WRITE(*,*)  a, b, c
         ELSE                      !      c <= b : a < c <= b
            WRITE(*,*)  a, c, b
         END IF
      ELSE                         !   a >= c    : c <= a < b
         WRITE(*,*) c, a, b
      END IF
   ELSE                            ! b <= a here
      IF (b < c) THEN              !   b < c     : b the smallest
         IF (a < c) THEN           !     a < c   : b <= a < c
            WRITE(*,*)  b, a, c
         ELSE                      !     a >= c  : b < c <= a
            WRITE(*,*)  b, c, a
         END IF
      ELSE                         !   c <= b    : c <= b <= a
         WRITE(*,*)  c, b, a
      END IF
   END IF

END PROGRAM  Order
Click here to download this program.

Discussion

This is a good example for using the box trick. The main idea is that if we know the smallest number, then one comparison between the remaining two would give the second smallest and the largest number. Finding the smallest of three numbers has been discussed in nested IF.

So, let us start with a:

a < b a may be the smallest
b may be the smallest

For the upper rectangle, we need to know if a is less than c, while for the lower rectangle we need to know if b is less than c:

a < b a < c a is the smallest, relation between b and c unknown
c <= a < b here, sorted!!!
b < c b is the smallest, relation between a and c unknown
c <= b <= a here, sorted!!!

For the top rectangle, we need one more comparison b < c and for the rectangle on the third row we need a < c. The following is our final result:

a < b a < c b < c a < b < c here, sorted!!!
a < c <= b here, sorted
c <= a < b here, sorted!!!
b < c a < c b <= a < c here, sorted!!!
b < c <= a here, sorted!!!
c <= b <= a here, sorted!!!


The above can be simplified a little if you use LOGICAL operators as discussed in nested IF. Here is the box diagram:

a < b and a < c b < c a < b < c here
a < b <= c here
b < a and b < c a < c b < a < c here
b < c <= a here
c < a and c < b a < b c < a < b here
c < b <= a here

Converting this diagram to IF-THEN-ELSE IF we have the following:

! -------------------------------------------------------
! This program reads in three INTEGERs and displays them
! in ascending order.
! -------------------------------------------------------

PROGRAM  Order
   IMPLICIT  NONE

   INTEGER  :: a, b, c

   READ(*,*)  a, b, c

   IF (a <= b .AND. a <= c) THEN   ! a the smallest
      IF (b <= c) THEN             !   a <= b <= c
         WRITE(*,*)  a, b, c
      ELSE                         !   a <= c <= b
         WRITE(*,*)  a, c, b
      END IF
   ELSE IF (b <= a .AND. b <= c) THEN  ! b the smallest
      IF (a <= c) THEN             !   b <= a <= c
         WRITE(*,*)  b, a, c
      ELSE                         !   b <= c <= a
         WRITE(*,*)  b, c, a
      END IF
   ELSE                            ! c the smallest
      IF (a <= b) THEN             !   c <= a <= b
         WRITE(*,*)  c, a, b
      ELSE                         !   c <= b <= a
         WRITE(*,*)  c, b, a
      END IF
   END IF

END PROGRAM  Order
Click here to download this program.