Good evening,
I'm currently writing a program that uses the heapsort algorithm to sort a very large two-dimensional array. However, I'm having problems with the program.
What I'm attempting to do is to sort the second column of a two dimensional array. So for instance, suppose that I sort this 2D array:
123 48
32 203
134 94
294 49
... ..
The end result would be
123 48
294 49
134 94
32 203
... ...
Here is my code:
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
When I run this program, I get an error that the array subscript was out-of-bounds. Any suggestions?
I'm currently writing a program that uses the heapsort algorithm to sort a very large two-dimensional array. However, I'm having problems with the program.
What I'm attempting to do is to sort the second column of a two dimensional array. So for instance, suppose that I sort this 2D array:
123 48
32 203
134 94
294 49
... ..
The end result would be
123 48
294 49
134 94
32 203
... ...
Here is my code:
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
When I run this program, I get an error that the array subscript was out-of-bounds. Any suggestions?