Advertisement
Guest User

Untitled

a guest
May 30th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MergerSort
  4. {
  5.     public static void clearScreen()
  6.     {
  7.       System.out.print('\u000C');
  8.     }
  9.     public  void main(String[] args) //2 arr for chack
  10.     {
  11.         Scanner scan = new Scanner (System.in);
  12.         clearScreen();
  13.         int[] a = {1,2,3,4,5};
  14.         int[] b = {1,2,3,4,5};
  15.     }
  16.     public static int isPermutation (int a[], int b[])
  17.     {
  18.         if (mergeSort(a)==mergeSort(b))
  19.         return 1;
  20.         else
  21.         return 0;
  22.     }
  23.     public static int isPermutation1 (int a[], int b[])
  24.     {
  25.        int i=0;
  26.        while (a[i++]!=b[i++])
  27.         {
  28.             return 0;
  29.         }
  30.         return 1;
  31.     }
  32.     public static void mergeSort(Comparable [ ] a)
  33.     {
  34.         Comparable[] tmp = new Comparable[a.length];
  35.         mergeSort(a, tmp,  0,  a.length - 1);
  36.     }
  37.  
  38.  
  39.     private static void mergeSort(Comparable [ ] a, Comparable [ ] tmp, int left, int right)
  40.     {
  41.         if( left < right )
  42.         {
  43.             int center = (left + right) / 2;
  44.             mergeSort(a, tmp, left, center);
  45.             mergeSort(a, tmp, center + 1, right);
  46.             merge(a, tmp, left, center + 1, right);
  47.         }
  48.     }
  49.  
  50.  
  51.     private static void merge(Comparable[ ] a, Comparable[ ] tmp, int left, int right, int rightEnd )
  52.     {
  53.         int leftEnd = right - 1;
  54.         int k = left;
  55.         int num = rightEnd - left + 1;
  56.  
  57.         while(left <= leftEnd && right <= rightEnd)
  58.             if(a[left].compareTo(a[right]) <= 0)
  59.                 tmp[k++] = a[left++];
  60.             else
  61.                 tmp[k++] = a[right++];
  62.  
  63.         while(left <= leftEnd)    // Copy rest of first half
  64.             tmp[k++] = a[left++];
  65.  
  66.         while(right <= rightEnd)  // Copy rest of right half
  67.             tmp[k++] = a[right++];
  68.  
  69.         // Copy tmp back
  70.         for(int i = 0; i < num; i++, rightEnd--)
  71.             a[rightEnd] = tmp[rightEnd];
  72.     }
  73.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement