fork download
  1. ! qsort_reals.f90 --
  2. !
  3. ! Example belonging to "Modern Fortran in Practice" by Arjen Markus
  4. !
  5. ! This work is licensed under the Creative Commons Attribution 3.0 Unported License.
  6. ! To view a copy of this license, visit http://c...content-available-to-author-only...s.org/licenses/by/3.0/
  7. ! or send a letter to:
  8. ! Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
  9. !
  10. ! Compact implementation of the QuickSort algorithm
  11. !
  12. ! Note:
  13. ! Because the function uses Fortran 90 features, its interface should be made
  14. ! explicit when using it in an actual program. This is easiest via a module.
  15. !
  16. module qsort_functions
  17. implicit none
  18. contains
  19. recursive function qsort_reals( data ) result( sorted )
  20. real, dimension(:), intent(in) :: data
  21. real, dimension(1:size(data)) :: sorted
  22.  
  23. if ( size(data) > 1 ) then
  24. sorted = &
  25. (/ qsort_reals( pack( data(2:), data(2:) > data(1) ) ), &
  26. data(1), &
  27. qsort_reals( pack( data(2:), data(2:) <= data(1) ) ) /)
  28. else
  29. sorted = data
  30. endif
  31. end function qsort_reals
  32. end module qsort_functions
  33.  
  34.  
  35. !Тестирующая программа
  36. program sort
  37. use qsort_functions
  38. implicit none
  39. real, dimension (10000000) :: a
  40.  
  41. call random_number(a)
  42. a = qsort_reals(a)
  43.  
  44. end program sort
Success #stdin #stdout 6.4s 258516KB
stdin
Standard input is empty
stdout
Standard output is empty