Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

sorting an array

sorting an array

sorting an array


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.

C        OPEN(UNIT=20,FILE='temp.txt',STATUS='old')
        DO 70 K=1,N
80      CONTINUE
               DO 90 J=1,N
                         IF (INPUT(J,2).GT.INPUT(J+1,2))THEN
C                        swop x values
90            CONTINUE
          DO 100 J=1,N
          IF (INPUT(J,2).LT.INPUT(J+1,2))THEN
              GOTO 100
              GOTO 80
100            CONTINUE
         DO 110 i=1,N
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

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

Hi Mullins

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close