Advertisement
iamaamir

isDuplicate

Nov 15th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4. /*
  5.  * To change this license header, choose License Headers in Project Properties.
  6.  * To change this template file, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9. /**
  10.  *
  11.  * @author Aamir khan
  12.  */
  13. public class NoDuplicates {
  14.  
  15.     public static void main(String[] args) {
  16.         int[] a = new int[100];
  17.         int[] b = new int[100];
  18.        
  19.         fillRandom(a);
  20.         fillRandom(b);
  21.        
  22.         //lets sortBoth first
  23.         new NoDuplicates().sortBoth(a, b);
  24.        
  25.         //now find equals
  26.         boolean isEquals = equals(a, b);
  27.        
  28.         System.out.println("is a equals b: "+ isEquals);
  29.        
  30.        
  31.     }
  32.  
  33.     /**
  34.      * Returns <tt>true</tt> if the two specified arrays of ints are
  35.      * <i>equal</i> to one another. Two arrays are considered equal if both
  36.      * arrays contain the same number of elements, and all corresponding pairs
  37.      * of elements in the two arrays are equal. In other words, two arrays are
  38.      * equal if they contain the same elements in the same order. Also, two
  39.      * array references are considered equal if both are <tt>null</tt>.<p>
  40.      *
  41.      * @param a one array to be tested for equality
  42.      * @param a2 the other array to be tested for equality
  43.      * @return <tt>true</tt> if the two arrays are equal
  44.      */
  45.     public static boolean equals(int[] a, int[] a2) {
  46.         if (a == a2) {
  47.             return true;
  48.         }
  49.         if (a == null || a2 == null) {
  50.             return false;
  51.         }
  52.  
  53.         int length = a.length;
  54.         if (a2.length != length) {
  55.             return false;
  56.         }
  57.  
  58.         for (int i = 0; i < length; i++) {
  59.             if (a[i] != a2[i]) {
  60.                 return false;
  61.             }
  62.         }
  63.  
  64.         return true;
  65.     }
  66.  
  67.    
  68.    
  69.    
  70.     /**
  71.      Fill Array with some random numbers
  72.      */
  73.     private static void fillRandom(int[] a) {
  74.         for (int i = 0, len = a.length; i < len; i++)
  75.             a[i] = new java.util.Random().nextInt();
  76.     }
  77.  
  78.     /**
  79.      * Sorts the specified array into ascending numerical order.
  80.      *
  81.      * <p>
  82.      * Implementation note: The sorting algorithm is a Dual-Pivot Quicksort A
  83.      * much better implementation can be found here
  84.      * http://www.docjar.com/html/api/java/util/DualPivotQuicksort.java.html
  85.      *
  86.      * @param a the array to be sorted
  87.      */
  88.     public void sortBoth(int[] a, int[] b) {
  89.         QuickSort quickSort = new QuickSort();
  90.         quickSort.sort(a);
  91.         quickSort.sort(b);
  92.     }
  93.  
  94.     class QuickSort {
  95.  
  96.         private int array[];
  97.         private int length;
  98.  
  99.         public void sort(int[] inputArr) {
  100.  
  101.             if (inputArr == null || inputArr.length == 0) {
  102.                 return;
  103.             }
  104.             this.array = inputArr;
  105.             length = inputArr.length;
  106.             quickSort(0, length - 1);
  107.         }
  108.  
  109.         private void quickSort(int lowerIndex, int higherIndex) {
  110.  
  111.             int i = lowerIndex;
  112.             int j = higherIndex;
  113.             // calculate pivot number, I am taking pivot as middle index number
  114.             int pivot = array[lowerIndex + (higherIndex - lowerIndex) / 2];
  115.             // Divide into two arrays
  116.             while (i <= j) {
  117.                 /**
  118.                  * In each iteration, we will identify a number from left side
  119.                  * which is greater then the pivot value, and also we will
  120.                  * identify a number from right side which is less then the
  121.                  * pivot value. Once the search is done, then we exchange both
  122.                  * numbers.
  123.                  */
  124.                 while (array[i] < pivot) {
  125.                     i++;
  126.                 }
  127.                 while (array[j] > pivot) {
  128.                     j--;
  129.                 }
  130.                 if (i <= j) {
  131.                     swap(i, j);
  132.                     //move index to next position on both sides
  133.                     i++;
  134.                     j--;
  135.                 }
  136.             }
  137.             // call quickSort() method recursively
  138.             if (lowerIndex < j) {
  139.                 quickSort(lowerIndex, j);
  140.             }
  141.             if (i < higherIndex) {
  142.                 quickSort(i, higherIndex);
  143.             }
  144.         }
  145.  
  146.         private void swap(int i, int j) {
  147.             int temp = array[i];
  148.             array[i] = array[j];
  149.             array[j] = temp;
  150.         }
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement