c----------------------------------------------------------- c Chapter 15: Sorting(p135) c----------------------------------------------------------- c Name of subroutine: HPSORT c c Algorithm:Sort a linear array into nondecreasing order. c c input: n=5 c complier: f77 hpsort_2.f c----------------------------------------------------------- program driver integer n, b(100) integer i write(6,10) 10 format('Please input the number of array to be sorted:') read(5,*),n write(6,20) 20 format('Please input the number:') do 50 i=1,n read(5,*) b(i) 50 continue call hpsort(n,b) write(6,60) 60 format('The sorted array is:') do 90 i=1,n write(6,80) b(i) 80 format(5i5) 90 continue end c-----Subroutine begins here-------------------------------- subroutine hpsort(n,b) integer b(500),bstar n1=n l=1+n/2 11 l=l-1 bstar=b(l) goto 30 25 bstar=b(n1) b(n1)=b(1) 29 n1=n1-1 30 l1=l 31 m=2*l1 if(m-n1)32,33,37 32 if(b(m+1).ge.b(m)) m=m+1 33 if(bstar.ge.b(m))goto 37 b(l1)=b(m) l1=m goto 31 37 b(l1)=bstar if(l.gt.1)goto 11 if(n1.ge.2)goto 25 return end