      program permheapsort ! bernd berg, dec 13, 2012.
c generation of a permutation n! using random numbers and heapsort.
      include '../../libs/fortran/implicit.sta'
      parameter(iuo=6,iud=8,miu=-iuo,isd1=01,isd2=0,n=40,ng=10)
      dimension x(n),iper(n)
c
      m=n/ng
      if(ng*m.ne.n) stop "adjust n to multiple of ng"
      call rmaset(miu,iud,isd1,isd2,"ranmar.d")
      do i=1,n
        x(i)=rmafun()
      enddo
      call rmasave(iud,"ranmar.d")
      call heap_per(n,x,iper)
      write(iuo,'(/,"Seed used isd1 =",i2,". \\bigskip\\bigskip")') isd1
      do ig=1,ng
        i=(ig-1)*m
        write(iuo,'(/,10x,"Group \\#",I3.2,":",4I4.2,". \\bigskip")') 
     &                                       ig,(iper(i+j),j=1,m)
      enddo
      write(iuo,'(/,"\\end{document}")') 
c
      stop
      end

      include '../../libs/fortran/rmafun.f'
      include '../../libs/fortran/rmasave.f'
      include '../../libs/fortran/rmaset.f'
      include '../../libs/fortran/ranmar.f'

      include '../../libs/fortran/heap_per.f'
