Coursera algorithms week2 Basic sort interview Questions:2 permutation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.