Fortran Coder

查看: 6690|回复: 5

[通用算法] 用快速排序方法时出现的问题

[复制链接]

13

帖子

2

主题

0

精华

入门

F 币
74 元
贡献
44 点
发表于 2018-3-7 12:49:54 | 显示全部楼层 |阅读模式
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
请问是哪里出错了,非常感谢

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2018-3-7 15:39:40 | 显示全部楼层

13

帖子

2

主题

0

精华

入门

F 币
74 元
贡献
44 点
 楼主| 发表于 2018-3-7 17:06:28 | 显示全部楼层
好的,非常感谢您,我试一试

790

帖子

2

主题

0

精华

大宗师

F 币
3765 元
贡献
2255 点
发表于 2018-3-10 12:25:31 | 显示全部楼层
ivf自带快速排序。

13

帖子

2

主题

0

精华

入门

F 币
74 元
贡献
44 点
 楼主| 发表于 2018-3-12 15:50:05 | 显示全部楼层
ivf自带的快速排序是什么?可以通过排序特征值,排序响应的特征向量吗?谢谢您

790

帖子

2

主题

0

精华

大宗师

F 币
3765 元
贡献
2255 点
发表于 2018-3-17 10:41:43 | 显示全部楼层
可以。通过对自定义结构体排序就行。具体可以看ivf的帮助。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 12:37

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表