少し改良。配列のサイクリックシフトで。
module m_isort2 implicit none contains subroutine isort2y( x ) real, intent(in out) :: x(:) integer :: j, k, n n = size(x) if (n <= 1) return do k = n - 1, 1, -1 do j = k + 1, n if ( x(k) < x(j) ) exit end do x(k:j - 1) = cshift( x(k:j - 1), 1 ) end do return end subroutine isort2y 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 isort2y(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