Fortran实现数组的快速排序 | 您所在的位置:网站首页 › fortran冒泡法 › Fortran实现数组的快速排序 |
program testQuickSort
! PURPOSE - to test the QuickSort
implicit none
integer :: n, m, i
real,allocatable, dimension(:) :: nums
real :: ran_num
! Read in a number - N
write(*,*) 'Pls give the length (N) of the array (N should be the integer times of 5)'
read(*,*) n
allocate(nums(n))
! Generate the random array of the length N in the scope of (0 to N)
do i=1,n
call random_number(ran_num)
nums(i) = ran_num*n
end do
! Print the Original array before the sorting
write(*,*) '-------------------------------------------'
write(*,*) 'The original Array (left to right):'
m = 0
do while (m 1) then
V = A(1)
do i=1, n
if (A(i) V) then
n_high = n_high+1
high(n_high) = A(i)
else
n_equal = n_equal+1
equal(n_equal) = A(i)
end if
end do
if (n_low>1) call QuickSort(low(1:n_low),n_low)
if (n_high>1)call QuickSort(high(1:n_high),n_high)
if(n_low>0) A(1:n_low) = low(1:n_low)
if(n_equal>0) A(n_low+1:n_low+n_equal) = equal(1:n_equal)
if(n_high>0) A(n_low+n_equal+1:n) = high(1:n_high)
return
else
return
end if
end subroutine
说明: 因为插入代码的模板都没有FORTRAN,所以代码显示的关键字有些问题,但结构没变。 !------------------------------------------------------------- 测试结果如下:
|
CopyRight 2018-2019 实验室设备网 版权所有 |