sort_array_integer Subroutine

private recursive subroutine sort_array_integer(g, d, itabref)

Sort a vector of integers

Arguments

Type IntentOptional Attributes Name
integer(kind=I4), intent(in) :: g

left index

integer(kind=I4), intent(in) :: d

right index

integer(kind=I4), intent(inout), dimension(:) :: itabref

vector to sort


Source Code

   recursive subroutine sort_array_integer(g, d, itabref)
   !! Sort a vector of integers
   implicit none
   integer(kind=I4), intent(in   )               :: g          !! *left index*
   integer(kind=I4), intent(in   )               :: d          !! *right index*
   integer(kind=I4), intent(inout), dimension(:) :: itabref    !! *vector to sort*

      integer(kind=I4) :: i, j, mil
      integer(kind=I4) :: tmp, cle

      i = g
      j = d
      mil = (g+d)/2
      cle = itabref(mil)

      if (g>=d) return

      do while (i<=j)
         do while (itabref(i)<cle)
            i = i + 1
         enddo
         do while (itabref(j)>cle)
            j = j - 1
         enddo
         if (i<=j) then
            ! échange des éléments du tableau
            tmp = itabref(i)
            itabref(i) = itabref(j)
            itabref(j) = tmp

            ! échange des éléments du vecteur position
            i = i + 1
            j = j - 1
         endif
      enddo

      if (g<j) call sort_array_integer(g, j, itabref)
      if (d>i) call sort_array_integer(i, d, itabref)

   return
   endsubroutine sort_array_integer