Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program shaker
- integer::n,m
- integer,allocatable::x(:,:)
- open (101,file= 'array.txt',status='old')
- read(101,*) n
- read(101,*) m
- allocate (x(1:n,1:m))
- print*, 'Source::'
- do i=1,n
- read(101,*) x(i,:)
- print*, x(i,:)
- end do
- close (101)
- print*,'Results'
- do i=1,m
- call sort(n,x(:,i)) !В подпрограмму отправляется столбец
- end do
- do i=1,n
- print*, x(i,:)
- end do
- deallocate (x)
- end program shaker
- subroutine sort(array_size,array)
- integer i,j
- integer last_unsorted, first_unsorted, exchange
- logical way,exchanged
- integer,intent(in) :: array_size
- integer,intent(inout) :: array(array_size)
- last_unsorted = array_size
- first_unsorted = 1
- exchanged= .true. !Определяет,происходила ли перестановка
- way = .true. !Определяет путь обхода
- do while (exchanged)
- exchanged= .false.
- if (way) then
- do i=first_unsorted,last_unsorted-1
- if (array(i) .gt. array(i+1)) then
- exchange = array(i)
- array(i) = array(i+1)
- array(i+1) = exchange
- exchanged= .true.
- end if
- end do
- last_unsorted = last_unsorted -1
- else
- do i=last_unsorted-1,first_unsorted,-1
- if (array(i) .gt. array(i+1)) then
- exchange = array(i)
- array(i) = array(i+1)
- array(i+1) = exchange
- exchanged= .true.
- end if
- end do
- first_unsorted = first_unsorted +1
- end if
- way = .not. way
- if (first_unsorted .ge. last_unsorted) exit
- end do
- end subroutine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement