Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Sorting Algorithm

Status
Not open for further replies.

gjw1684

Vendor
May 26, 2007
9
US
Hi,

I have a question regarding use of a sorting algorithm. Suppose I have a file as follows:

103 32
329 45
340 24
334 43
452 40

What I would like to do is sort each row in ascending order based on the second column value. So if I wanted to sort this, I would have

340 24
103 32
452 40
334 43
329 45

Any suggestions on how to do this?
 
It looks like a homework (see this forum rules;). May be you will present your (initial) code then discuss your problems?
 
Here's how my initial code looks. My biggest problem is that I'm not sure how to put all of my values into "temp" and place them into a 2-D array correctly. One thought on correcting this would be to use two different arrays instead. Any suggestions?

program sort

real :: temp
integer :: row = 5
integer :: column = 2
integer :: i, j, m, n, k, l, iptr, status

real,dimension(row,column) :: a

open(unit=5, file='test.txt', status='old')
open(unit=6, file='result.txt', status='replace')

if (status == 0) then

do
do
read(5,*,iostat=status) temp
n = n+1
m = m+1
endo
a(m,n) = temp
endo


do = i+1, m
iptr=i
do j=1, n-1
if(a(i,j).LT.a(i,iptr)) then
temp=a(i,j)
a:),i)=a:),iptr)
a(i,iptr)=temp
end if
end do
end do

end if

write(6,*) ((a(k,l), l=1,column),k=1,row)

end program sort
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top