      subroutine elfsort(thresh)
      use grid_module
      implicit none
      integer indxt,i,j,l,ir
      real thresh,q
      do i=1, npts
         indx(i)=i
      enddo
      if (npts.gt.1) then
         l=npts/2+1
         ir=npts
10       continue
           if(l.gt.1) then
              l=l-1
              indxt=indx(l)
              q=elf(indxt)
           else
              indxt=indx(ir)
              q=elf(indxt)
              indx(ir)=indx(1)
              ir=ir-1
              if(ir.eq.1) then
                 indx(1)=indxt
                 go to 30
              endif
           endif
           i=l
           j=l+l
*
           if(q.ge.thresh) then
20         if(j.le.ir) then
              if(j.lt.ir) then
                 if(elf(indx(j)).gt.elf(indx(j+1))) j=j+1
              endif
              if(q.gt.elf(indx(j))) then
                 indx(i)=indx(j)
                 i=j
                 j=j+j
              else
                 j=ir+1
              endif
              go to 20
           endif
*
           else
              j=ir+1
           endif
           indx(i)=indxt
           go to 10
30         continue
      endif
      return
      end
