Advertisement
starm100

shaker_sort

Apr 2nd, 2019
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program shaker
  2. integer::n,m
  3. integer,allocatable::x(:,:)
  4. open (101,file= 'array.txt',status='old')
  5. read(101,*) n                            
  6. read(101,*) m                            
  7. allocate (x(1:n,1:m))              
  8. print*, 'Source::'
  9. do i=1,n
  10.     read(101,*) x(i,:)
  11.     print*, x(i,:)
  12. end do
  13. close (101)
  14. print*,'Results'
  15. do i=1,m
  16.     call sort(n,x(:,i)) !В подпрограмму отправляется столбец
  17. end do
  18. do i=1,n
  19.     print*, x(i,:)
  20. end do
  21. deallocate (x)
  22. end program shaker
  23.  
  24.     subroutine sort(array_size,array)
  25.     integer i,j
  26.     integer last_unsorted, first_unsorted, exchange
  27.     logical way,exchanged
  28.     integer,intent(in)      :: array_size
  29.     integer,intent(inout)   :: array(array_size)
  30.     last_unsorted = array_size
  31.     first_unsorted = 1
  32.     exchanged= .true. !Определяет,происходила ли перестановка
  33.     way = .true. !Определяет путь обхода
  34.     do while (exchanged)
  35.         exchanged= .false.
  36.         if (way) then
  37.             do i=first_unsorted,last_unsorted-1
  38.                 if (array(i) .gt. array(i+1)) then
  39.                     exchange   = array(i)
  40.                     array(i)   = array(i+1)
  41.                     array(i+1) = exchange
  42.             exchanged= .true.
  43.                 end if
  44.             end do
  45.             last_unsorted = last_unsorted -1
  46.         else
  47.             do i=last_unsorted-1,first_unsorted,-1
  48.                 if (array(i) .gt. array(i+1)) then
  49.                     exchange   = array(i)
  50.                     array(i)   = array(i+1)
  51.                     array(i+1) = exchange
  52.             exchanged= .true.
  53.                 end if
  54.             end do
  55.             first_unsorted = first_unsorted +1
  56.         end if
  57.         way = .not. way
  58.         if (first_unsorted .ge. last_unsorted) exit
  59.     end do
  60. end subroutine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement