Original title:
Given the arrays of size n, design a subquadratic algorithm to determine whether one is a permutation of the othe R. is, does they contain exactly the same entries but, possibly, in a different order.
In essence, the two arrays are sorted to be equal, since this lesson is selected, inserted, Hill Sort, choose the most unfamiliar hill sort to achieve it
1 Importjava.util.Arrays;2 3 Public classPermutationarrays {4 Private int[] A;5 Private int[] b;6 Private intN;7Permutationarrays (intNint[] A,int[] b) {8 This. A =A;9 This. B =b;Ten This. N =N; One } A Private BooleanLessintVintW) { - returnV <W; - } the Private voidExch (int[] A,intIintj) { - intt =A[i]; -A[i] =A[j]; -A[J] =T; + } - Private voidSortbyshell (intNint[] a) { + intH = 1; A while(H < N/3){ atH = 3*h + 1; - } - while(h>=1){ - for(inti = h; i<n;i++){ - for(intj = i; J>=h && Less (a[j],a[j-h]); j=j-h) { -Exch (a,j,j-h); in } - } toH = H/3; + } - } the Public Booleanispermutation () { * Sortbyshell (n,a); $ System.out.println (Arrays.tostring (a));Panax Notoginseng Sortbyshell (n,b); - System.out.println (arrays.tostring (b)); the for(inti=0;i<n;i++){ + if(A[i]! =B[i]) A return false; the } + return true; - } $ Public Static voidMain (string[] args) { $ int[] A = {1,2,4,5,6,11,9,7,8,0}; - int[] B = {0,9,8,7,6,5,4,3,2,1}; -Permutationarrays PA =NewPermutationarrays (10, A, b); the System.out.println (Pa.ispermutation ()); - }Wuyi}
Coursera algorithms week2 Base sort interview Questions:2 permutation