看板 Fortran 關於我們 聯絡資訊
找到副程式如下 (來源:https://gist.github.com/t-nissie/479f0f16966925fa29ea) --------------------------------------------------- recursive subroutine quicksort(a, first, last) implicit none real*8 a(*), x, t integer first, last integer i, j x = a( (first+last) / 2 ) i = first j = last do do while (a(i) < x) i=i+1 end do do while (x < a(j)) j=j-1 end do if (i >= j) exit t = a(i); a(i) = a(j); a(j) = t i=i+1 j=j-1 end do if (first < i-1) call quicksort(a, first, i-1) if (j+1 < last) call quicksort(a, j+1, last) end subroutine quicksort --------------------------------------------------- 自己試寫了一下主程式如下 --------------------------------------------------- program quick_sort_test real*8 a(*) integer first, last data a/12.0,54.0,65.0,2.0,3.0,40.0,91.0,7.0,321.0,50.0/ first=0 last=22 call quicksort(a, first, last) end program --------------------------------------------------- 因為不太懂 1. real*8 a(*) 的星號 2. first, last 的意義 結果一直顯示array boundary有問題 調了 first 和 last 也還是一樣 主程式應該要怎麼修改呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.76.232.247 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Fortran/M.1590375785.A.145.html ※ 編輯: BanPeeBan (42.76.232.247 臺灣), 05/25/2020 11:04:07
fragmentwing: 如果我沒弄錯 你這樣參考點會直接選到a(11)吧 可是 05/25 11:52
fragmentwing: 你的數列是不是只有10個值 05/25 11:52
fragmentwing: 先試著其他別動,把last改成20看看? 05/25 11:53
fragmentwing: 痾不 改成10好了 他應該是想抓無序下排在中間的值當 05/25 11:59
fragmentwing: 參考值吧 05/25 11:59
espresso1: a(*)是在副程式裡假設陣列大小的用法 05/26 20:46
espresso1: 不能用在主程式,主程式的陣列要有明確的上下限 05/26 20:48
espresso1: data有10個a就宣告10或以上 05/26 20:51
espresso1: first, last就a是要傳遞給副程式的上下界 05/26 20:53
espresso1: 這裡就是1跟10,表示傳遞a(1)到a(10) 05/26 20:55
espresso1: 因此a(*)改為a(10) 05/26 21:03
espresso1: first=1, last=10 05/26 21:04