question about permut-by-sorting
- by davit-datuashvili
hi i have following question
from book introduction in algorithms second edition there is such problem
suppose we have some array A
int a[]={1,2,3,4} and we have some random priorities array
P={36,3,97,19} we shoud permut array a randomly using this priorities array
here is pseudo code
P ERMUTE -B Y-S ORTING ( A)
1 n ? length[A]
2 for i ? 1 to n
do P[i] = R ANDOM(1, n 3 )
3
4 sort A, using P as sort keys
5 return A
and result will be permuted array
B={2, 4, 1, 3};
please help any ideas
i have done this code and need aideas how continue
import java.util.*;
public class Permut {
public static void main(String[]args){
Random r=new Random();
int a[]=new int[]{1,2,3,4};
int n=a.length;
int b[]=new int[a.length];
int p[]=new int[a.length];
for (int i=0;i<p.length;i++){
p[i]=r.nextInt(n*n*n)+1;
}
// for (int i=0;i<p.length;i++){
// System.out.println(p[i]);
//}
}
}
please help