fortran90实现查找排序的函数
时间: 2023-11-27 20:03:43 浏览: 23
我想您可能是想问"计数排序",因为查找排序不是一个常见的排序算法。以下是Fortran90实现计数排序的函数示例:
```fortran
subroutine counting_sort(arr, n)
implicit none
integer, intent(inout) :: arr(n)
integer, value :: n, i, j, k, min_val, max_val, range
integer, dimension(:), allocatable :: count
! Determine the range of values in the array
min_val = minval(arr)
max_val = maxval(arr)
range = max_val - min_val + 1
! Allocate memory for the count array
allocate(count(range))
count = 0
! Count the occurrences of each value in the array
do i = 1, n
count(arr(i)-min_val+1) = count(arr(i)-min_val+1) + 1
end do
! Compute cumulative sums in the count array
do i = 2, range
count(i) = count(i) + count(i-1)
end do
! Sort the array using the count array
do i = n, 1, -1
j = count(arr(i)-min_val+1)
arr(j) = arr(i)
count(arr(i)-min_val+1) = count(arr(i)-min_val+1) - 1
end do
! Deallocate the count array
deallocate(count)
end subroutine counting_sort
```
该函数接受一个整数数组arr和数组长度n作为参数,并将数组按升序进行计数排序。计数排序是一种非比较排序算法,它利用一个额外的计数数组来统计每个元素在原始数组中出现的次数,从而得到一个有序的序列。计数排序的时间复杂度为O(n+k),其中k是数值范围,但它需要额外的空间来存储计数数组。