1

Through f2py -c sorting.f90 -m sortF, I get sortF.cpython-37m-x86_64-linux-gnu.so which is great.

sorting.f90

module sorting 
    use iso_fortran_env, only: i4 => int32, i8 => int64
    implicit none
contains
    subroutine bubbleSort(array)
        implicit none
        integer, dimension(:):: array
        integer :: i, j, tem
        i = size(array)
        do while (i > 1)
            do j = 1, i - 1 
                if (array(j) > array(j + 1)) then
                    tem = array(j)
                    array(j) = array(j + 1)
                    array(j + 1) = tem
                end if 
            end do
            i = i - 1
        end do 
    end subroutine bubbleSort
end module sorting 

In this subroutine, the input array is changed, therefore, I expect after I execute sorting.bubblesort(x), I hope x becomes [1, 2, 3, 5]

In [1]: from sortF import sorting                                                                                   

In [2]: x = [1, 2, 5, 3]                                                                                            

In [3]: print(sorting.bubblesort(x))                                                                                
None

In [4]: x                                                                                                           
Out[4]: [1, 2, 5, 3]
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
  • 1
    If you change you definition of `x` to `x = np.array([1, 2, 5, 3], dtype=np.int32)` it gives the expected result. So `x` needs to be a numpy array, not a list, and you would have to declare `array` (and `tem`) as `integer(i8)` on the Fortran side (see [docs](https://numpy.org/doc/stable/f2py/advanced.html#dealing-with-kind-specifiers) or [here](https://stackoverflow.com/a/61869003/3967096) for details on that) if you want to use Python's default (64bit) integers. – jbdv Aug 26 '21 at 07:58
  • 1
    @jbdv Wow, intriguing fact to me, thanks a lot. – ComplicatedPhenomenon Aug 26 '21 at 10:37

0 Answers0