Sorting an Array

Problem Statement

Sorting is another important computer application. Write a program to reorder the elements of a given array into ascending order.

There are many sorting methods. The following presents the so-called selection sort.

The idea is simple. Suppose the array has n elements. We find the smallest element and its location in the array and swap this element with the first one. After this, the first element is the smallest. Then, find the smallest element and its location of all elements in the range of 2 and n and swap this one with the second. After this, the first two elements are sorted. Repeating this process, when we have elements 1 to i-1 sorted, we can find the the smallest element in the range of i and n and swap it with element i. Then, elements 1 to i are sorted. This process continues until the first n-1 elements are sorted, the whole array is sorted.

Solution

! --------------------------------------------------------------------
! PROGRAM  Sorting:
!    This program can sort a set of numbers.  The method used is
! usually referred to as "selection" method.
! --------------------------------------------------------------------

PROGRAM  Sorting
   IMPLICIT  NONE
   INTEGER, PARAMETER :: MAX_SIZE = 100
   INTEGER, DIMENSION(1:MAX_SIZE) :: InputData
   INTEGER                        :: ActualSize
   INTEGER                        :: i

   READ(*,*)  ActualSize, (InputData(i), i = 1, ActualSize)
   WRITE(*,*) "Input Array:"
   WRITE(*,*) (InputData(i), i = 1, ActualSize)

   CALL  Sort(InputData, ActualSize)

   WRITE(*,*)
   WRITE(*,*) "Sorted Array:"
   WRITE(*,*) (InputData(i), i = 1, ActualSize)

CONTAINS

! --------------------------------------------------------------------
! INTEGER FUNCTION  FindMinimum():
!    This function returns the location of the minimum in the section
! between Start and End.
! --------------------------------------------------------------------

   INTEGER FUNCTION  FindMinimum(x, Start, End)
      IMPLICIT  NONE
      INTEGER, DIMENSION(1:), INTENT(IN) :: x
      INTEGER, INTENT(IN)                :: Start, End
      INTEGER                            :: Minimum
      INTEGER                            :: Location
      INTEGER                            :: i

      Minimum  = x(Start)               ! assume the first is the min
      Location = Start                  ! record its position
      DO i = Start+1, End               ! start with next elements
         IF (x(i) < Minimum) THEN       !   if x(i) less than the min?
            Minimum  = x(i)             !      Yes, a new minimum found
            Location = i                !      record its position
         END IF
      END DO
      FindMinimum = Location            ! return the position
   END FUNCTION  FindMinimum

! --------------------------------------------------------------------
! SUBROUTINE  Swap():
!    This subroutine swaps the values of its two formal arguments.
! --------------------------------------------------------------------

   SUBROUTINE  Swap(a, b)
      IMPLICIT  NONE
      INTEGER, INTENT(INOUT) :: a, b
      INTEGER                :: Temp

      Temp = a
      a    = b
      b    = Temp
   END SUBROUTINE  Swap

! --------------------------------------------------------------------
! SUBROUTINE  Sort():
!    This subroutine receives an array x() and sorts it into ascending
! order.
! --------------------------------------------------------------------

   SUBROUTINE  Sort(x, Size)
      IMPLICIT  NONE
      INTEGER, DIMENSION(1:), INTENT(INOUT) :: x
      INTEGER, INTENT(IN)                   :: Size
      INTEGER                               :: i
      INTEGER                               :: Location

      DO i = 1, Size-1                  ! except for the last
         Location = FindMinimum(x, i, Size)     ! find min from this to last
         CALL  Swap(x(i), x(Location))  ! swap this and the minimum
      END DO
   END SUBROUTINE  Sort

END PROGRAM  Sorting
Click here to download this program.

Program Input and Output

Let the input to this program be:
10
5  4  2  9  0
3  6  8  7  1
The output of the program is:
Input Array:
5,  4,  2,  9,  0,  3,  6,  8,  7,  1

Sorted Array:
0,  1,  2,  3,  4,  5,  6,  7,  8,  9

Discussion