program sort_test ! !*********************************************************************** ! !! SORT_TEST tests out a sorting routine. ! implicit none ! integer, parameter :: n = 10 real, dimension(n) :: a real ahi real alo integer i ! call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'SORT_TEST' write ( *, '(a)' ) ' Demonstrate the use of RVEC_SORT_BUBBLE' write ( *, '(a)' ) ' to sort a real array using bubble sort.' do i = 1, 2 alo = 10.0E+00 ahi = 25.0E+00 call rvec_random ( alo, ahi, n, a ) call rvec_print ( n, a, ' Unsorted array:' ) call rvec_sort_bubble ( n, a ) call rvec_print ( n, a, ' Sorted array:' ) end do write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'SORT_TEST:' write ( *, '(a)' ) ' Normal end of execution.' stop end subroutine rvec_random ( alo, ahi, n, a ) ! !*********************************************************************** ! !! RVEC_RANDOM sets a real vector to random values. ! ! ! Discussion: ! ! The random values are chosen by a call to the intrinsic routine ! RANDOM_NUMBER. Hence, the user may set the seed by a call to ! RANDOM_SEED. ! ! Modified: ! ! 07 February 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ALO, AHI, the range of values desired in the vector. ! ! Input, integer N, the dimension of the vector. ! ! Output, real A(N), random values between ALO and AHI. ! implicit none ! integer, intent ( in ) :: n real, intent ( in ) :: ahi real, intent ( in ) :: alo real, dimension ( n ), intent ( out ) :: a integer :: i call random_number ( a ) a = alo + a * ( ahi - alo ) end subroutine rvec_sort_bubble ( n, a ) ! !*********************************************************************** ! !! RVEC_SORT_BUBBLE sorts a real vector using bubble sort. ! ! ! Modified: ! ! 07 February 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the dimension of the vector. ! ! Input/output, real A(N), the vector to be sorted. ! implicit none ! integer, intent ( in ) :: n integer :: i integer :: j real, dimension ( n ), intent ( inout ) :: a do i = 1, n do j = i+1, n if ( a(i) > a(j) ) then call r_swap ( a(i), a(j) ) end if end do end do return end subroutine r_swap ( a1, a2 ) ! !*********************************************************************** ! !! R_SWAP swaps two real values ! ! Modified: ! ! 07 February 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input/output, real A1, A2, two real values to swap. ! implicit none ! real, intent ( inout ) :: a1 real, intent ( inout ) :: a2 real :: a3 a3 = a1 a1 = a2 a2 = a3 return end subroutine rvec_print ( n, a, title ) ! !******************************************************************************* ! !! RVEC_PRINT prints a real vector. ! ! ! Modified: ! ! 16 December 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, real A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title to be printed first. ! TITLE may be blank. ! implicit none ! integer n ! real a(n) integer i character ( len = * ) title ! if ( title /= ' ' ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(i6,g14.6)' ) i, a(i) end do return end subroutine timestamp ( ) ! !******************************************************************************* ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! ! Example: ! ! May 31 2001 9:45:54.872 AM ! ! Modified: ! ! 31 May 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none ! character ( len = 8 ) ampm integer d character ( len = 8 ) date integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s character ( len = 10 ) time integer values(8) integer y character ( len = 5 ) zone ! call date_and_time ( date, time, zone, values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & trim ( month(m) ), d, y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end