Fortran code for sorting a data array
This program can be used to sort a set of data array either in ascending or descending order.
program sort_data
implicit none
integer, parameter :: array_length = 100
integer :: i, count
double precision, dimension(1:array_length) :: data
double precision :: temp
logical :: not_sorted
open(unit=1, file='data.input', action='read', status='old')
read(1,*) (data(i), i=1, array_length)
not_sorted = .TRUE.
do while (not_sorted)
count = 0
do i = 1, state - 1
temp = data(i)
! if one needs to get ascending order need to change here .LT. to .GT.
if (data(i) .LT. data(i + 1)) then
data(i) = data(i+1)
data(i+1) = temp
count = count
else
count = count + 1
end if
end do
if (count == (state - 1)) not_sorted = .FALSE.
end do
end program sort_data
not_sorted = .TRUE.
do while (not_sorted)
count = 0
do i = 1, state - 1
temp = data(i)
! if one needs to get ascending order need to change here .LT. to .GT.
if (data(i) .LT. data(i + 1)) then
data(i) = data(i+1)
data(i+1) = temp
count = count
else
count = count + 1
end if
end do
if (count == (state - 1)) not_sorted = .FALSE.
end do
end program sort_data
Comments
Post a Comment