Advertisement
Oppaceted

Insertion right-to-left sort

Apr 27th, 2023
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ! 4 sort, 136 pseudo random
  2. program hello
  3.     use My_module
  4.     implicit none
  5.     integer :: n, comparison, permutations, i
  6.     real, allocatable :: array(:)
  7.     real :: time_start, time_end
  8.     write (0,'(40x,a)') '>>>INSERTION SORT<<<'
  9.     !
  10.     call cpu_time(time_start)
  11.     !
  12.     do i =1,13,1
  13.         !write (0, fmt = '(a21$)') 'Enter size of array: '
  14.         !read (*,fmt = *) n
  15.         n = 2**i
  16.         !
  17.         allocate(array(n))
  18.         !call random_array(n, array, 0.0, 100.0)
  19.         call pseudo_random_array(n, array, 2.0, 8192.0)
  20.         !call already_sorted(n, array, 2.0, 8192.0)
  21.         !call WriteArray(n,array, 1, n, 0)
  22.         call insertion_sort(n, array, comparison, permutations)
  23.         !call WriteArray(n,array, 1, n, 0)
  24.         write (0,fmt = '(/,a,i3)') 'Step number:', i
  25.         write (0,fmt = '(a,i10,/,a,i10)') 'Comparison: ', comparison, 'Permutations', permutations
  26.         deallocate(array)
  27.         !
  28.     end do
  29.     !
  30.     call cpu_time(time_end)
  31.     write (0,fmt = '(/,a,f11.6)' ) 'Execution time: ',( time_end - time_start )
  32. end program
  33.  
  34. module My_module
  35.     implicit none
  36.     contains
  37.     !
  38.     subroutine random_array(n, array, minimum, maximum)
  39.         integer :: n, i
  40.         real :: minimum, maximum, array(n), temp
  41.         do i =1, n
  42.             call random_number(temp)
  43.             array(i) = minimum + (maximum-minimum)*temp
  44.         end do
  45.     end subroutine
  46.     !
  47.     subroutine pseudo_random_array(n, array, low, high)
  48.         integer :: n, i
  49.         real :: array(n), S, low, high
  50.         real, parameter :: pi = 3.1415926535897932384626
  51.         real :: fractional, x
  52.         fractional(x) = x - real(int(x))
  53.         !call cpu_time(S)
  54.         S = 0.712345635224536436
  55.         do i =1, n
  56.             s = fractional( (s+pi)**5 )
  57.             array(i) = low + (high - low)*s
  58.         end do
  59.     end subroutine
  60.     !
  61.     subroutine already_sorted(n, array, low, high)
  62.         integer :: n, i
  63.         real :: array(n), low, high
  64.         do i =1, n
  65.             array(i) = low + (high - low)*(n-i+1)/n
  66.         end do
  67.     end subroutine
  68.     !
  69.     subroutine ReadArray(n, array)
  70.         integer :: i, n
  71.         real, dimension(n) :: array
  72.         do i =1,n
  73.             write (*,fmt = '(/a, i2, a$)') 'Enter element number ', i, ': '
  74.             read (*,*) array(i)
  75.         end do
  76.     end subroutine
  77.     !
  78.     subroutine WriteArray(n, array, first, last, spec)
  79.         integer :: i, n, first, last, spec
  80.         real, dimension(n) :: array
  81.         character(len=31) :: cap = '+-----------+-----------------+'
  82.         write (spec,'(a)') cap
  83.         write (spec,'(a)') '|     i     |       a[i]      |'
  84.         do i=first, last
  85.             write (spec,'(a)') cap
  86.             write (spec,'(a, i5, 3x, a, f15.7, 2x, a)') '|   ', i, '|', array(i), '|'
  87.         end do
  88.         write (spec,'(a/)') cap
  89.     end subroutine
  90.     !
  91.     subroutine insertion_sort(n, array, comparison, permutations)
  92.         integer :: n, i, k, comparison, permutations
  93.         real :: array(n), temp
  94.         comparison = 0
  95.         permutations = 0
  96.         if (n == 1) then
  97.             return
  98.         end if
  99.         do i = (n-1),1,-1
  100.             temp = array(i)
  101.             k = i + 1
  102.             do while (k <= n .and. array(k) < temp )
  103.                 comparison = comparison + 1
  104.                 array(k-1)=array(k)
  105.                 permutations = permutations + 1
  106.                 k = k + 1
  107.             end do
  108.             array(k-1) = temp
  109.             comparison = comparison + 1
  110.             !write (0,*) array
  111.         end do
  112.     end subroutine
  113. end module
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement