subroutine Hbuild( Ha, Hj, Hk, N, Nk, hops )
integer(ip), intent(in) :: N, Nk
integer(ip), intent(inout) :: Hj(N), Hk(Nk)
integer(ip), intent(out) :: hops
real(rp), intent(inout) :: Ha(N)
!==================================================================
! Hbuild initializes the heap by inserting each element of Ha.
! Input: Ha, Hj.
! Output: Ha, Hj, Hk, hops.
!
! 01 May 2002: Use k for new length of heap, not k-1 for old length.
! 05 May 2002: Use kk in call to stop loop variable k being altered.
! (Actually Hinsert no longer alters that parameter.)
! 07 May 2002: ftnchek wants us to protect Nk, Ha(k), Hj(k) too.
! 07 May 2002: Current version of Hbuild.
! 12 Dec 2011: First f90 version.
!==================================================================
integer(ip) :: h, jv, k, kk, Nkk
real(rp) :: v
Nkk = Nk
hops = 0
do k = 1, N
kk = k
v = Ha(k)
jv = Hj(k)
call Hinsert( Ha, Hj, Hk, kk, Nkk, v, jv, h )
hops = hops + h
end do
end subroutine Hbuild