arrayssortingfortranfortran90

# Sorting an array from lowest to greatest FORTRAN

So I'm in my first year of the carrer and I've gotten stuck in an assignment, we need to read an array and then give it sorted from lowest to highest, so my approach was to create another array and star giving it the first array's minimum value and go on. Little me I did it, I made a code so that I could read the array's values and assign the lowest to a separate value called minimo. However I'm stuck right there, I can assign that lowest value to the first value of my new sorted array, but when checking again for the lowest value it would of course tell me the same number, so I thought that maybe after giving me the lowest value I could remove it from the original array and check again for the lowest value, but I literally don't know where to start, or even if this is the optimal way to do it, or even a possible one. Here's the code.

``````PROGRAM hello
IMPLICIT NONE
integer, dimension(1000) :: vector, v_ascendente, v_descendente
integer :: v_size, i, j, minimo = 1000000

print*, "Cuantos numeros introducira"
print*, "Introduzca los", v_size, "numeros del vector separandolos por ENTER"

do i=1, v_size
end do

do j=1, v_size
do i=1, v_size
minimo = min(minimo, vector(i))
end do
print*, "minimo = ", minimo
v_ascendente(j) = minimo
end do
END PROGRAM
``````

You guys got any idea on another way to do this? Thanks in advance.

Solution

• Leaving aside for a moment whether or not this is a good way to sort an array let's answer OP's immediate question concerning how to make the chosen approach work. This answer uses the intrinsic routines `minval` and `minloc`. In both cases it uses the optional argument `mask` which governs which elements of an array argument are considered. For a proper explanation of the functions, and of their arguments, see your favourite Fortran documentation.

First, declare the mask, a logical array of the same size and shape as `vector`

``````  LOGICAL, DIMENSION(v_size) :: mk = .TRUE.
``````

Next, repeat the following logic `v_size` times:

• find the smallest element in `vector` which has not been masked off;
• copy that element to the `i`-th location in `v_ascendente`;
• mask off the location of that smallest element so that it is not considered next time around.

``````DO ix = 1, v_size