subroutine APVT(a, n, order, jcol, kodPivot, prag)
! Cauta pivotul in coloana "jcol" (liniile jcol:n), astfel ca:
! kodPivot /=0:  a(i,i) sa fie maxim
! kodPivot  =0:  primul a(i,i) /=0
!
! Schimba liniile in A si ordinea lor in ORDER
implicit real(8) (a-h, o-z)

real(8) a(n, n)
integer order(n)
common no_interchange, kodPrint
integer width

! Initializari:
pivot =0
irow_max =jcol

! Cauta pivotul in col. "jcol" - sub elementul diagonal (inclusiv acesta):
PVT_DO: DO irow =jcol, n

! Schimba liniile "jcol" si "irow":
if(irow >jcol) then
call swap_row(n, a, order, jcol, irow)
endif


! Check for singular:
if(jcol ==1) then
a_ii =a(1,1)

else
call compute_a_ii(n, a, jcol, a_ii)
endif

print_if_0: if(abs(a_ii) <prag .and. kodPrint >2) then
	iwn =width(n); iwj =width(jcol); iwi =width(irow);
	write(1, '(/, a, I<iwi>, a, <n>(I<iwn>, 1x))') ' pas ' , jcol, ' (test) - order: ', order(jcol:n)
	write(1, '(a, I<iwj>, a, I<iwj>, a, 1pg15.7)') ' u(', jcol, ',', jcol, ') = ', a_ii
endif print_if_0


IF(abs(a_ii) >abs(pivot)) then
irow_max =irow
pivot =a_ii

	if(kodPivot ==0) then
	if(irow>jcol) no_interchange =no_interchange +1
	RETURN
	endif

!ELSE
! Restore & Cycle
ENDIF

! Restore pivoting and continue:
if(irow >jcol) then
call swap_row(n, a, order, irow, jcol)
endif

ENDDO PVT_DO


! Final pivoting - for maximal pivot:
if(irow_max >jcol) then
call swap_row(n, a, order, jcol, irow_max)
no_interchange =no_interchange +1
endif

END

