LU Decomposition with Pivoting

First here is a complete LU decomposition with partial pivoting.

LU[A_,0]:=Module[{LU=A, n=Length[A],d,e,r,i,j},
Do[
d=1/LU[[r,r]];
Do[
e = d LU[[i,r]];
LU[[i,r]]=e;
Do[
LU[[i,j]]-= e* LU[[r,j]],
{j,r+1,n}
],
{i,r+1,n,1}],
{r,1,n-1,1}];
{LowerTriangularize[LU,-1]+SparseArray[Band[{1,1}]->1,{n,n}], 
UpperTriangularize[LU]}
]

LUPivot[A_,0]:=Module[{LU=A, n=Length[A],perm,pivot,d,e,r,i,j},
perm=Table[i,{i,n}];
Do[
(* Partial Pivoting *)
pivot=Ordering[Abs[LU[[r;;n,r]]],-1][[1]]+r-1;
perm[[{r,pivot}]]=perm[[{pivot,r}]];
LU[[{r,pivot}]]=LU[[{pivot,r}]];
(* End Partial Pivoting *)
d=1/LU[[r,r]];
Do[
e = d LU[[i,r]];
LU[[i,r]]=e;
Do[
LU[[i,j]]-= e* LU[[r,j]],
{j,r+1,n}
],
{i,r+1,n,1}],
{r,1,n-1,1}];
{LowerTriangularize[LU,-1]+SparseArray[Band[{1,1}]->1,{n,n}], 
UpperTriangularize[LU], perm}
]

n=5;
A=RandomReal[{-1,1},{n,n}];
{L,U,perm}=LUPivot[A,0];
P=IdentityMatrix[n][[perm]];
{MatrixForm[L.U], MatrixForm[P.A], MatrixForm[A[[perm]]]}
{MatrixForm[Transpose[P].L.U], MatrixForm[A]}

n=20;
SampleSize=500;
Data=Transpose[
Table[
A=RandomReal[{-1,1},{n,n}];
{LP,UP,perm}=LUPivot[A,0];
{L,U}=LU[A,0];
{Norm[L.U-A],Norm[LP.UP-A[[perm]]]},
{SampleSize}]
];
Histogram[Log[10,Data]]

n=50;
SampleSize=500;
Data=Transpose[
Table[
A=RandomReal[{-1,1},{n,n}];
{LP,UP,perm}=LUPivot[A,0];
{L,U}=LU[A,0];
{Norm[L.U-A],Norm[LP.UP-A[[perm]]]},
{SampleSize}]
];
Histogram[Log[10,Data]]