配列生成子を使ったものは、配列はみ出しの場合分けをしなくて済むが、死ぬほど遅いw 素朴な方がいい。
module m_isort2 contains subroutine isort2( x ) real, intent(in out) :: x(:) real :: tmp integer :: i, k, n n = size(x) if (n <= 1) return do k = n - 1, 1, -1 tmp = x(k) do j = k + 1, n if ( tmp > x(j) ) then x(j - 1) = x(j) else exit end if end do x(j - 1) = tmp end do return end subroutine isort2 subroutine isort2x( x ) real, intent(in out) :: x(:) real :: tmp integer :: i, k, n n = size(x) if (n <= 1) return do k = n - 1, 1, -1 tmp = x(k) do j = k + 1, n if ( tmp < x(j) ) exit end do x(k:) = [ x(k + 1:j - 1), tmp, x(j:) ] ! array constructor end do return end subroutine isort2x end module program sort2 use m_isort2 implicit none real, allocatable :: x(:) integer :: i ! allocate( x(10**5) ) call random_seed() call random_number(x) call isort2x(x) print *, x do i = 1, size(x) - 1 ! check if (x(i) > x(i + 1) ) print *, 'error!', i end do stop end program sort2