# 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

• Function FindMinimum() receives array x(), a starting location Start, and an ending location End, and returns the location of the smallest element in this range.
• This function starts with assuming the first element, x(Start) is the smallest one and saves its location Start to Location.
• The DO-loop checks all subsequent elements. For each element, it is compared with Minimum, the current minimum. If it is smaller than the current minimum, the current minimum is updated with this new value and Location is updated with its location. Therefore, at the end of this loop, Minimum and Location will contain the minimum value of this section and its location.
• Subroutine Swap() interchanges the values of its formal arguments.
• Subroutine Sort() takes an array x() and its size Size. For each element between the first and the one next to the last (i.e., Size-1), the location of the smallest element in the range of i and Size is found using function FindMinimum(). Then, subroutine Swap() is called to interchange the values of x(i) and x(Location). After this, elements in the range of 1 and i are sorted.