看板 Fortran 關於我們 聯絡資訊
我今天有一些資料 可能是這樣子 0.1 -0.2 -0.1 0.4 0.5 -0.3 -0.4 0.6 然後我要將他們的絕對值從大到小排序 變成 0.1 -0.1 -0.2 -0.3 0.4 -0.4 0.5 0.6 一開始我把資料都讀進陣列 然後想說用氣泡排序掛ABS去排排看結果排不出來 所以想請問大家 該怎麼寫程式碼 這是原本的code subroutine sub3() implicit none integer ,parameter :: N=15165 real:: A(N) integer:: i,j ! open(11,file="di.txt") open(12,file="db.txt" ,status='replace') ! do i=1,15165 read(11,"(F5.1)") A(i) end do call BUBBLE_SORT(A,N) do j=1,15165 write(12,"(F5.1)") A(i) end do close(11) close(12) end subroutine !//////氣泡排序副程式/// subroutine BUBBLE_SORT(A,N) implicit none integer :: N integer I,J, TEMP real :: A(N) do I=N-1,1,-1 ! 開始做N-1次的掃瞄 do J=1,I ! 一對一對的來比較,I之後的數字不用比較 ! 如果ABS(A(J)) >ABS( A(J+1)) 就把這兩個數值交換 if (ABS(A(J))>ABS(A(J+1))) then TEMP=A(J) A(J)=A(J+1) A(J+1)=TEMP end if end do end do return write(*,*) A(123) end subroutine (氣泡排序的副程式是抓FORTRAN95這本書的副程式來改的) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.195 ※ 編輯: roopp 來自: 140.112.4.195 (10/01 14:01) ※ 編輯: roopp 來自: 140.112.4.195 (10/01 14:23)
awer89:絕對值從大到小排序還是相反 10/01 16:58
gilocustom:A是浮點數,TEMP卻是整數? 10/01 17:01
awer89:恩 TEMP那裏有誤 10/01 17:13