sorting an array
sorting an array
(OP)
Hi,
I want to sort an array by colunm 2 swopping the values of the whole row each time a sweep is made. for some reason it will sort the input array (given at the bottom) if i only want to swop the values in colum 2 but when i add the code to swop the other columns it gets mixed up. Any help would be appreciated. this is my code.
PROGRAM SORT_NODES
INTEGER:: I,J,SWOPS,NODE,NUM
REAL:: INPUT(10,4),TEMP,TEMPY,TEMPZ,TEMPN,X,Y,Z
OPEN(UNIT=40,FILE='input.txt',STATUS='old')
C OPEN(UNIT=20,FILE='temp.txt',STATUS='old')
OPEN(UNIT=30,FILE='newinput.txt',STATUS='unknown')
N=10
DO 70 K=1,N
READ(40,*)NODE,X,Y,Z
INPUT(K,1)=NODE
INPUT(K,2)=X
INPUT(K,3)=Y
INPUT(K,4)=Z
70 CONTINUE
80 CONTINUE
DO 90 J=1,N
IF (INPUT(J,2).GT.INPUT(J+1,2))THEN
C swop x values
TEMP=INPUT(J,2)
TEMPN=INPUT(J,1)
TEMPY=INPUT(J,3)
TEMPZ=INPUT(J,4)
INPUT(J,1)=INPUT(J+1,1)
INPUT(J,2)=INPUT(J+1,2)
INPUT(J,3)=INPUT(J+1,3)
INPUT(J,4)=INPUT(J+1,4)
INPUT(J+1,2)=TEMP
INPUT(J+1,1)=TEMPN
INPUT(J+1,3)=TEMPY
INPUT(J+1,4)=TEMPZ
C
ENDIF
90 CONTINUE
DO 100 J=1,N
IF (INPUT(J,2).LT.INPUT(J+1,2))THEN
GOTO 100
ELSE
GOTO 80
ENDIF
100 CONTINUE
DO 110 i=1,N
NUM=INT(INPUT(i,1))
WRITE(30,*)num,INPUT(i,2),INPUT(i,3),INPUT(i,4)
C DO 110 I=4,10
C WRITE(20,*)NODE
C INPUT(I,1)=NODE
C INPUT(I,2)=X
C INPUT(I,3)=Y
C INPUT(I,4)=Z
110 CONTINUE
STOP
END PROGRAM SORT_NODES
And my sample input file:
5867, 87.74, -71.2088, -6.77245
5868, 22.9593, 110.643, -6.52507
5869, -111.578, -17.872, -232.855
5870, -3.09514, 112.958, -144.851
5871, 108.945, 29.9988, -4.97048
5872, 24.106, -110.399, -191.622
5873, -34.0516, 107.747, -227.898
5874, -112.632, -9.11318, -235.768
5875, -108.809, -30.4893, -235.772
5876, 111.631, 17.5379, -38.9666
5877, -24.8057, 110.244, -7.1432
5878, -23.8124, 110.463, -228.046
5879, 12.1292, 112.347, -6.46787
I want to sort an array by colunm 2 swopping the values of the whole row each time a sweep is made. for some reason it will sort the input array (given at the bottom) if i only want to swop the values in colum 2 but when i add the code to swop the other columns it gets mixed up. Any help would be appreciated. this is my code.
PROGRAM SORT_NODES
INTEGER:: I,J,SWOPS,NODE,NUM
REAL:: INPUT(10,4),TEMP,TEMPY,TEMPZ,TEMPN,X,Y,Z
OPEN(UNIT=40,FILE='input.txt',STATUS='old')
C OPEN(UNIT=20,FILE='temp.txt',STATUS='old')
OPEN(UNIT=30,FILE='newinput.txt',STATUS='unknown')
N=10
DO 70 K=1,N
READ(40,*)NODE,X,Y,Z
INPUT(K,1)=NODE
INPUT(K,2)=X
INPUT(K,3)=Y
INPUT(K,4)=Z
70 CONTINUE
80 CONTINUE
DO 90 J=1,N
IF (INPUT(J,2).GT.INPUT(J+1,2))THEN
C swop x values
TEMP=INPUT(J,2)
TEMPN=INPUT(J,1)
TEMPY=INPUT(J,3)
TEMPZ=INPUT(J,4)
INPUT(J,1)=INPUT(J+1,1)
INPUT(J,2)=INPUT(J+1,2)
INPUT(J,3)=INPUT(J+1,3)
INPUT(J,4)=INPUT(J+1,4)
INPUT(J+1,2)=TEMP
INPUT(J+1,1)=TEMPN
INPUT(J+1,3)=TEMPY
INPUT(J+1,4)=TEMPZ
C
ENDIF
90 CONTINUE
DO 100 J=1,N
IF (INPUT(J,2).LT.INPUT(J+1,2))THEN
GOTO 100
ELSE
GOTO 80
ENDIF
100 CONTINUE
DO 110 i=1,N
NUM=INT(INPUT(i,1))
WRITE(30,*)num,INPUT(i,2),INPUT(i,3),INPUT(i,4)
C DO 110 I=4,10
C WRITE(20,*)NODE
C INPUT(I,1)=NODE
C INPUT(I,2)=X
C INPUT(I,3)=Y
C INPUT(I,4)=Z
110 CONTINUE
STOP
END PROGRAM SORT_NODES
And my sample input file:
5867, 87.74, -71.2088, -6.77245
5868, 22.9593, 110.643, -6.52507
5869, -111.578, -17.872, -232.855
5870, -3.09514, 112.958, -144.851
5871, 108.945, 29.9988, -4.97048
5872, 24.106, -110.399, -191.622
5873, -34.0516, 107.747, -227.898
5874, -112.632, -9.11318, -235.768
5875, -108.809, -30.4893, -235.772
5876, 111.631, 17.5379, -38.9666
5877, -24.8057, 110.244, -7.1432
5878, -23.8124, 110.463, -228.046
5879, 12.1292, 112.347, -6.46787
RE: sorting an array
Your problem is very trivial, your do loops DO 90 J=1,N and DO 100 J=1,N cause an array out of bounds error on the very next line in each case you try to reference INPUT(J+1,2) which means when J equals N you try to reference INPUT(N+1,2)which obviously doesn't exist. Changing the loops to J=1,N-1 corrects the error.
However I must add that your code is basically the notorious "BUBBLE" sort method, it's okay for very small arrays (<12) only. The method is probably the most inefficient algorithm in the whole history of computer programming ! The run time for this method increases exponentially with the size of the array and only a relatively modest size array can bog your computer down for hours or even days!! Any decent book on programming techniques should provide the source code for the very excellent HEAP or SHELL sort methods.