Fortran Coder

标题: 用快速排序方法时出现的问题 [打印本页]

作者: 学客    时间: 2018-3-7 12:49
标题: 用快速排序方法时出现的问题
module qsort_c_module
      implicit none
      public :: QsortC
      private :: Partition
      contains
      recursive subroutine QsortC(A,B)
      real, intent(in out), dimension(:) :: A
      real, intent(in out), dimension(:,:) :: B
      integer :: iq
      if(size(A) > 1) then
      call Partition(A, iq, B)
      call QsortC(A(:iq-1),B(:,:iq-1))
      call QsortC(A(iq:),B(:,iq:))
      endif
      end subroutine QsortC
      subroutine Partition(A, marker, B)
      real, intent(in out), dimension(:) :: A
      real, intent(in out), dimension(:,:) :: B
      integer, intent(out) :: marker
      integer :: i, j
      real :: temp
      real ,dimension(1:size(A))::temb
      real :: x      ! pivot poin
      x = A(1)
      i= 0
      j= size(A) + 1
      do
          j = j-1
      do
      if (A(j) <= x) exit
      j = j-1
      end do
      i = i+1
      do
        if (A(i) >= x) exit
        i = i+1
      end do
      if (i < j) then
        ! exchange A(i) and A(j)
        temp = A(i)
        temb = B(:,i)
        A(i) = A(j)
        B(:,i)=B(:,j)
        A(j) = temp
        B(:,j)=temb
      elseif (i == j) then
        marker = i+1
        return
      else
        marker = i
        return
      endif
      end do
      end subroutine Partition
      end module qsort_c_module
      program www_fcode_cn
      use qsort_c_module
      implicit none
      integer, parameter :: r = 3
      real, dimension(1:r) :: myarray = (/0, 50, 20/)
      real, dimension(1:r,1:r) :: mb=(/1,2,3,4,5,6,7,8,9/)
      write(2,*) "原数组:", myarray
      write(2,'*') "原数组:", mb
      call QsortC(myarray,mb)
      write(2,*) "排序后:", myarray
      write(2,'*') "排序后:", mb
      end program www_fcode_cn

用这个修改后的快速排序法,排列特征值A,和特征向量B,出现了错误:
B排序输出后,出现了这样的数字:1.000000       2.000000       3.000000       7.000000   
   8.000000       9.000000       4.000000       5.000000     -1.5823616E-26
请问是哪里出错了,非常感谢

作者: pasuka    时间: 2018-3-7 15:39
先调用LAPACK自带的快排函数吧
https://software.intel.com/en-us ... nce-fortran-p-lasrt
https://software.intel.com/en-us ... rence-fortran-lasrt
作者: 学客    时间: 2018-3-7 17:06
好的,非常感谢您,我试一试
作者: li913    时间: 2018-3-10 12:25
ivf自带快速排序。
作者: 学客    时间: 2018-3-12 15:50
ivf自带的快速排序是什么?可以通过排序特征值,排序响应的特征向量吗?谢谢您
作者: li913    时间: 2018-3-17 10:41
可以。通过对自定义结构体排序就行。具体可以看ivf的帮助。




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2