Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ! 4 sort, 136 pseudo random
- program hello
- use My_module
- implicit none
- integer :: n, comparison, permutations, i
- real, allocatable :: array(:)
- real :: time_start, time_end
- write (0,'(40x,a)') '>>>INSERTION SORT<<<'
- !
- call cpu_time(time_start)
- !
- do i =1,13,1
- !write (0, fmt = '(a21$)') 'Enter size of array: '
- !read (*,fmt = *) n
- n = 2**i
- !
- allocate(array(n))
- !call random_array(n, array, 0.0, 100.0)
- call pseudo_random_array(n, array, 2.0, 8192.0)
- !call already_sorted(n, array, 2.0, 8192.0)
- !call WriteArray(n,array, 1, n, 0)
- call insertion_sort(n, array, comparison, permutations)
- !call WriteArray(n,array, 1, n, 0)
- write (0,fmt = '(/,a,i3)') 'Step number:', i
- write (0,fmt = '(a,i10,/,a,i10)') 'Comparison: ', comparison, 'Permutations', permutations
- deallocate(array)
- !
- end do
- !
- call cpu_time(time_end)
- write (0,fmt = '(/,a,f11.6)' ) 'Execution time: ',( time_end - time_start )
- end program
- module My_module
- implicit none
- contains
- !
- subroutine random_array(n, array, minimum, maximum)
- integer :: n, i
- real :: minimum, maximum, array(n), temp
- do i =1, n
- call random_number(temp)
- array(i) = minimum + (maximum-minimum)*temp
- end do
- end subroutine
- !
- subroutine pseudo_random_array(n, array, low, high)
- integer :: n, i
- real :: array(n), S, low, high
- real, parameter :: pi = 3.1415926535897932384626
- real :: fractional, x
- fractional(x) = x - real(int(x))
- !call cpu_time(S)
- S = 0.712345635224536436
- do i =1, n
- s = fractional( (s+pi)**5 )
- array(i) = low + (high - low)*s
- end do
- end subroutine
- !
- subroutine already_sorted(n, array, low, high)
- integer :: n, i
- real :: array(n), low, high
- do i =1, n
- array(i) = low + (high - low)*(n-i+1)/n
- end do
- end subroutine
- !
- subroutine ReadArray(n, array)
- integer :: i, n
- real, dimension(n) :: array
- do i =1,n
- write (*,fmt = '(/a, i2, a$)') 'Enter element number ', i, ': '
- read (*,*) array(i)
- end do
- end subroutine
- !
- subroutine WriteArray(n, array, first, last, spec)
- integer :: i, n, first, last, spec
- real, dimension(n) :: array
- character(len=31) :: cap = '+-----------+-----------------+'
- write (spec,'(a)') cap
- write (spec,'(a)') '| i | a[i] |'
- do i=first, last
- write (spec,'(a)') cap
- write (spec,'(a, i5, 3x, a, f15.7, 2x, a)') '| ', i, '|', array(i), '|'
- end do
- write (spec,'(a/)') cap
- end subroutine
- !
- subroutine insertion_sort(n, array, comparison, permutations)
- integer :: n, i, k, comparison, permutations
- real :: array(n), temp
- comparison = 0
- permutations = 0
- if (n == 1) then
- return
- end if
- do i = (n-1),1,-1
- temp = array(i)
- k = i + 1
- do while (k <= n .and. array(k) < temp )
- comparison = comparison + 1
- array(k-1)=array(k)
- permutations = permutations + 1
- k = k + 1
- end do
- array(k-1) = temp
- comparison = comparison + 1
- !write (0,*) array
- end do
- end subroutine
- end module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement