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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting

Status
Not open for further replies.

acharls

Technical User
May 20, 2013
9
IN
Hi all,

I have a sorting program

do i=1,4
do j=i+1,5
if(er(i).gt.er(j))then
temp=er(i)
er(i)=er(j)
er(j)=temp
end if
end do
end do

I am looking how I can print sorted values with their array index as in original data.

example
er=[9,3,2,5,1,4]
sorted er=[1,2,3,4,5,9] & print original index of elements
ie 5,3,2,6,4,1 while printing the sorted output.

Thanks in advance

Charls Antony
 
what you can do is put together another "index" array of the same size N as the array you want to sort, fill it in with the numbers 1 through N and, THEN, every time you do a swap in the sorted array, you make the same swap in the "index" array.
 
Use a type.
Code:
type (Pair)
   integer value ! er values
   integer index
end type

When initializing your array of Pair. Assume that PMAX is the size
Code:
do ii = 1, PMAX
   plist(ii)%index = ii
   plist(ii)%value = ??   ! how er(ii) got its value
end do

When sorting, the comparison is
Code:
if (plist(ii).value .lt. plist(jj).value) then ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top