Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 2D数组的气泡排序行_Fortran - Fatal编程技术网

Fortran 2D数组的气泡排序行

Fortran 2D数组的气泡排序行,fortran,Fortran,我正在做作业的第二部分,要求我对矩阵重新排序,使每一行的顺序是单调递增的,每一行的第一个元素是单调递增的。如果两行具有相同的初始值,则应按行中的第二个元素对行进行排序。如果这两个元素都相同,那么它应该是第三个元素,继续到最后一个元素 我已经编写了一个冒泡排序,它在第一部分工作得很好(重新排列每一行)。我为第二部分编写了一个冒泡排序(确保每行的第一个元素是单调递增的)。然而,我遇到了一个无限循环,我不明白为什么 我知道问题在于我的“inoder”变量最终没有设置为true(这将结束while循环)

我正在做作业的第二部分,要求我对矩阵重新排序,使每一行的顺序是单调递增的,每一行的第一个元素是单调递增的。如果两行具有相同的初始值,则应按行中的第二个元素对行进行排序。如果这两个元素都相同,那么它应该是第三个元素,继续到最后一个元素

我已经编写了一个冒泡排序,它在第一部分工作得很好(重新排列每一行)。我为第二部分编写了一个冒泡排序(确保每行的第一个元素是单调递增的)。然而,我遇到了一个无限循环,我不明白为什么

我知道问题在于我的“inoder”变量最终没有设置为true(这将结束while循环)。然而,我不明白为什么inorder没有设置为true。我的逻辑如下:一旦下面的代码交换了行,使所有行都处于有序状态,我们将再次通过while循环(inorder将设置为true),这将导致while循环结束。我不明白为什么这不会发生

inorder = .false.
loopA: do while ( .not. inorder ) !While the rows are not ordered
    inorder = .true.
        loopB: do i = 1, rows-1 !Iterate through the first column of the array
            if (arr(i,1)>arr(i+1,1)) then !If we find a row that is out of order
                inorder = .false.
                tempArr = arr(i+1,:) !Swap the corresponding rows
                arr(i+1,:) = arr(i,:)
                arr(i,:) = tempArr
            end if

            if (arr(i,1)==arr(i+1,1)) then !The first elements of the rows are the same
                loopC: do j=2, cols !Iterate through the rest of the row to find the first element that is not the same
                    if (arr(i,j)>arr(i+1,j)) then !Found elements that are not the same and that are out of order
                        inorder = .false.
                        tempArr = arr(i+1,:) !Swap the corresponding rows
                        arr(i+1,:) = arr(i,:)
                        arr(i,:) = tempArr
                    end if
                end do loopC
           end if
        end do loopB
end do loopA
输入示例:

  6    3    9   23   80
  7   54   78   87   87
 83    5   67    8   23
102    1   67   54   34
 78    3   45   67   28
 14   33   24   34    9
示例(正确)输出(我的代码未生成):


还有一种可能是,几个小时的盯着它看让我错过了一些愚蠢的事情,所以我很欣赏任何指针。

当你比较第一个元素相同的行时,你会遍历整个数组并比较每个元素

因此,如果有两个这样的数组:

1 5 3
1 2 4
然后第一个元素是相同的,它进入代码的第二部分

第二,5>2,所以它交换了它:

1 2 4
1 5 3
但它并没有停止。在第三位,4>3,所以它把它换回来了

1 5 3
1 2 4
现在你又回到了原来的位置

干杯

1 5 3
1 2 4