These subroutines were made to sort various data.
All these subroutines were built my self.
I tested it in Fortran 77, Fortran 95 in Ubuntu 9.XX and both of fortran compiles it well.
1. sort a simple data
-explanation for Input-
E - a real dimension with 'ns' data.
ns - an integer variable. It represents a number of data of another input 'E'.
-warring-
E wil be sorted and changed to be an output.
subroutine sort(ns,E)
real C,E(ns)
integer i,j
do i=1,ns
do j=1,ns-1
if (E(j+1).lt.E(j)) then
C=E(j)
E(j)=E(j+1)
E(j+1)=C
endif
enddo
enddo
return
end
2. sort two data
Input parameters are similar as a simple sort.
-explanation for Input-
E1,E2 - a real dimension with 'ns' data.
ns - an integer variable. It represents a number of data of another input 'E1','E2'.
-warring-
E1,E2 wil be sorted by E1 and changed to be an output.
subroutine sort2(ns,E1,E2)
real C1,C2,E1(ns),E2(ns)
integer i,j
do i=1,ns
do j=1,ns-1
if (E1(j+1).lt.E1(j)) then
C1=E1(j)
C2=E2(j)
E1(j)=E1(j+1)
E2(j)=E2(j+1)
E1(j+1)=C1
E2(j+1)=C2
endif
enddo
enddo
return
end
3. sort a 2nd dimension
-explanation for Input-
x- a real 2nd dimension with 'n' data.
n - an integer variable. It represents a number of data of another input 'x'.
-warring-
x wil be sorted by x(n,1) and changed to be an output.
subroutine 2ndsort(x,n)
real x(n,2),x1,x2
integer n
do i=1,n
do j=1,n-1
if(x(j+1,1).lt.x(j,1)) then
x1=x(j,1)
x2=x(j,2)
x(j,1)=x(j+1,1)
x(j,2)=x(j+1,2)
x(j+1,1)=x1
x(j+1,2)=x2
endif
enddo
enddo
return
end
4. sort a large size data
-explanation for Input-
val- a real dimension with 'nstar' data. The data 'ch' will be sorted by the value in this dimension.
ch - a character dimension which contains the data wrote in character.
nstar - an integer variable. It represents a number of data of another input 'val', 'ch'.
-warring-
val,ch wil be sorted by val and changed to be an output.
Be careful to have same size of character (ch, chref).
subroutine datasort(val,ch,n)
integer n
real val(n),ref
character ch(n)*250,chref*250
do i=1,nstar
do j=1,nstar-1
if(val(j+1).lt.val(j)) then
ref=val(j)
write(chref,'(a250)') ch(j)
val(j)=val(j+1)
write(ch(j),'(a250)') ch(j+1)
val(j+1)=ref
write(ch(j+1),'(a250)') chref
endif
enddo
enddo
return
end
All these subroutines were built my self.
I tested it in Fortran 77, Fortran 95 in Ubuntu 9.XX and both of fortran compiles it well.
1. sort a simple data
-explanation for Input-
E - a real dimension with 'ns' data.
ns - an integer variable. It represents a number of data of another input 'E'.
-warring-
E wil be sorted and changed to be an output.
subroutine sort(ns,E)
real C,E(ns)
integer i,j
do i=1,ns
do j=1,ns-1
if (E(j+1).lt.E(j)) then
C=E(j)
E(j)=E(j+1)
E(j+1)=C
endif
enddo
enddo
return
end
2. sort two data
Input parameters are similar as a simple sort.
-explanation for Input-
E1,E2 - a real dimension with 'ns' data.
ns - an integer variable. It represents a number of data of another input 'E1','E2'.
-warring-
E1,E2 wil be sorted by E1 and changed to be an output.
subroutine sort2(ns,E1,E2)
real C1,C2,E1(ns),E2(ns)
integer i,j
do i=1,ns
do j=1,ns-1
if (E1(j+1).lt.E1(j)) then
C1=E1(j)
C2=E2(j)
E1(j)=E1(j+1)
E2(j)=E2(j+1)
E1(j+1)=C1
E2(j+1)=C2
endif
enddo
enddo
return
end
3. sort a 2nd dimension
-explanation for Input-
x- a real 2nd dimension with 'n' data.
n - an integer variable. It represents a number of data of another input 'x'.
-warring-
x wil be sorted by x(n,1) and changed to be an output.
subroutine 2ndsort(x,n)
real x(n,2),x1,x2
integer n
do i=1,n
do j=1,n-1
if(x(j+1,1).lt.x(j,1)) then
x1=x(j,1)
x2=x(j,2)
x(j,1)=x(j+1,1)
x(j,2)=x(j+1,2)
x(j+1,1)=x1
x(j+1,2)=x2
endif
enddo
enddo
return
end
4. sort a large size data
-explanation for Input-
val- a real dimension with 'nstar' data. The data 'ch' will be sorted by the value in this dimension.
ch - a character dimension which contains the data wrote in character.
nstar - an integer variable. It represents a number of data of another input 'val', 'ch'.
-warring-
val,ch wil be sorted by val and changed to be an output.
Be careful to have same size of character (ch, chref).
subroutine datasort(val,ch,n)
integer n
real val(n),ref
character ch(n)*250,chref*250
do i=1,nstar
do j=1,nstar-1
if(val(j+1).lt.val(j)) then
ref=val(j)
write(chref,'(a250)') ch(j)
val(j)=val(j+1)
write(ch(j),'(a250)') ch(j+1)
val(j+1)=ref
write(ch(j+1),'(a250)') chref
endif
enddo
enddo
return
end