Vous êtes sur la page 1sur 3

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 READ(*,*) :: a, b, c a, b, c ! a < b here ! a < c ! b < c c ! b ! a >= c : c <= a < b c <= b : a < c <= b : a the smallest : a < b < c

IF (a < b) THEN IF (a < c) THEN IF (b < c) THEN WRITE(*,*) a, b, ELSE WRITE(*,*) a, c, END IF ELSE WRITE(*,*) c, a, b END IF ELSE IF (b < c) THEN IF (a < c) THEN WRITE(*,*) b, a, ELSE WRITE(*,*) b, c, END IF ELSE WRITE(*,*) c, b, a END IF END IF END PROGRAM Order

! b <= a here ! b < c : b the smallest ! a < c : b <= a < c c ! a ! c <= b : c <= b <= a a >= c : b < c <= a

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 may be the smallest b may be the smallest

a<b

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 is the smallest, relation between b and c unknown c <= a < b here, sorted!!! b is the smallest, relation between a and c unknown c <= b <= a here, sorted!!!

a<c a<b b<c

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 < c here, sorted!!! a < c <= b here, sorted b <= a < c here, sorted!!! b < c <= a here, sorted!!!

a<c a<b b<c

b<c

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

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 b < a and b < c a < c c < a and c < b a < b a < b < c here a < b <= c here b < a < c here b < c <= a here 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. ------------------------------------------------------Order

PROGRAM

IMPLICIT INTEGER READ(*,*)

NONE :: a, b, c 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

Vous aimerez peut-être aussi