You need to implement an in-place heap sort program Fortran. the program should
ID: 652044 • Letter: Y
Question
You need to implement an in-place heap sort program Fortran. the program
should have a function/procedure/subroutine that will do the main work. That is, to accept a list/array (or
anything appropriate) of integers and then sort it using the in-place heap sort algorithm. The heap sort
algorithm requires converting the list/array into a heap. Once the list/array is converted into a heap, you
need to print out the heap in tree-format as a string before continuing. This function/procedure/subroutine
should return the sorted list/array and the string if possible. while let the sorted list/array to be return via
Explanation / Answer
Program heapsort
Implicit None
integer, parameter:: column = 2
integer, parameter:: n = 10000
integer :: i, ir, j, l
integer :: status
real, dimension(n,column) :: ra, temp
open (unit=9,file='test.txt',status='old',iostat=status )
open (unit=10, file='result.txt',status='replace')
if (status == 0) then
read(9,*,iostat=status) ((temp(i,j),j=1,column),i=1,n)
ra = temp
l = (n/2)+1
ir = n
10 continue
if (l.gt.1) then
l = l-1
temp(l,2) = ra(l,2)
else
temp(ir,2)= ra(ir,2)
ra(ir,2) = ra(1,2)
ir = ir-1
if (ir.eq.1) then
ra(1,2) = temp(1,2)
continue
end if
end if
i=l
j=l+1
20 if (j.le.ir) then
if (j.lt.ir) then
if(ra(j,2).lt.ra(j+1,2)) j=j+1
end if
if (temp(j,2).lt.ra(j,2)) then
ra(i,2) = ra(j,2)
i=j
j=j+j
else
j=ir+1
end if
go to 20
end if
ra(i,2) = temp(i,2)
go to 10
end if
write(10,*) ra
end program heapsort
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.